How to configure VSCode with Rust for binary execution? - rust

Assuming that I'm editing the lession_1/src/main.rs file, I want to Ctrl+F5 (or just F5) keyboard shortcut and have the following command executed (as if I was running it from lession_1 directory):
cargo run
My project structure
├── lesson_1
│ ├── Cargo.toml
│ └── src
│ └── main.rs
├── lesson_2
│ ├── Cargo.toml
│ └── src
│ └── main.rs
├── lesson_3
│ ├── Cargo.toml
│ └── src
│ └── main.rs
├── .git
├── .gitignore
├── .pre-commit-config.yaml
├── .pre-commit-hooks.yaml
├── README.md
├── .secrets.baseline
└── Untitled.ipynb
What I tried
I followed up this article, however, I do get an error:
error: could not find `Cargo.toml` in `/home/me/projects/my_rust_project` or any parent directory
My config
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "/usr/bin/cargo",
"args": ["run", "${workspaceFolder}/<executable file>"],
"cwd": "${workspaceFolder}"
}
]
}
.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [{
"label": "cargo build",
"type": "shell",
"command": "cargo build",
"args": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "cargo run",
"type": "shell",
"command": "cargo",
"args": [
"run"
// "--release",
// "--",
// "arg1"
],
"group": {
"kind": "build",
"isDefault": true
}
}]
}

Due to the structure of your project, you have several Cargo package in your workspace.
In your launch.json, cwd is set to the workspace folder and Cargo doesn't know which package to run.
Replace "cwd": "${workspaceFolder}" by "cwd": "${relativeFileDirname}" and Cargo will be run from the folder containing the Rust file you are currently editing, and should easily find the associated Cargo.toml.
For the tasks.json, the same solution can apply, but it means you are only building the Cargo package you are currently editing and not all of them.

Answer suggested by #eggyal
Changing the .vscode/tasks.json as follows does the job:
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "/usr/bin/cargo",
"args": ["run", "${workspaceFolder}/<executable file>"],
"cwd": "${fileDirname}"
}
]
}
The key is changing the cwd from "${workspaceFolder}" to "${fileDirname}".
As suggested, see VSCode's Variables Reference for more information.

Related

tsconfig paths are not working in ts-node

I know this question has been asked a lot of times. But none of the solutions seems to work for me.
I have setup a project installing typescript and ts-node to run the project in dev.
The project structure is as follows
.
├── src
│ ├── utils
│ │ └── logger.ts
│ ├── custom
│ │ └── services
| | └── func.service.ts
│ └── index.ts
├── package.json
└── tsconfig.json
Below is my tsconfig.json file
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"outDir": "./dist",
"baseUrl": "./src",
"removeComments": true,
"incremental": true,
"esModuleInterop": true,
"paths": {
"#utils/*": [
"utils/*"
],
"#custom/*": [
"custom/*"
]
}
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
I am importing the utils/logger in the src/index.ts file as import { Log } from '#utils/logger'
so when I run the below command
$ yarn dev
(which is configured to run ts-node src/index)
I am getting the below error
$ ts-node src/index
Error: Cannot find module '#utils/logger'
...
code: 'MODULE_NOT_FOUND',
...
error Command failed with exit code 1.
Went through different answers in the site, but none of them worked.
Edit: The intellisense in vscode works fine and detects the paths correctly

npm peer dependencies not being enforced

I am trying to understand peer dependencies in npm, and could not understand the behavior below.
I have the following versions of node and npm :
$ npm -v
8.15.0
$ node -v
v16.17.0
The following directory structure:
peer-dep-test$ tree -I "node_modules"
.
├── library
│ ├── package-lock.json
│ └── package.json
└── project
├── package-lock.json
└── package.json
Library contains the following imports (#altostra/type-validations is used as an example small package here):
{
"name": "library",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"peerDependencies": {
"#altostra/type-validations": "2.6.5"
}
}
and project contains the following:
{
"name": "project",
"version": "1.0.0",
"main": "index.js",
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"#altostra/type-validations": "1.0.2",
"library": "file:../library"
}
}
The project project has a version of #altostra/type-validations which should be incompatible with library - but I get no error on doing npm install. npm list gives the following output:
peer-dep-test/project$ npm list --all
project#1.0.0 peer-dep-test/project
├─┬ #altostra/type-validations#1.0.2
│ └─┬ #reactivex/ix-es2015-cjs#3.0.2
│ ├── #types/node#13.13.52
│ └── tslib#1.14.1
└─┬ library#1.0.0 -> ./../library
└─┬ #altostra/type-validations#2.6.5
└─┬ #reactivex/ix-es2015-cjs#3.0.2
├── #types/node#13.13.52
└── tslib#1.14.1
As per my understanding of peer dependencies,
library should expect #altostra/type-validations#2.6.5 to be installed directly in project
#altostra/type-validations#1.0.2 should conflict with the peerDependency of library, and either warn or error.
Am I missing something here?

Typescript builds files with incorrect path in monorepo

I have an example monorepo with 3 packages: back, front and shared. With the following folder structure:
root
├── back
│ ├── dist
│ ├── src
│ ├── test.ts
│ ├── package.json
│ └── tsconfig.json
├── front
├── shared
│ ├── dist
│ ├── src
│ ├── my-enum.ts
│ ├── package.json
│ └── tsconfig.json
├── package.json
└── tsconfig.json
test.ts
import { MyEnum } from '#test/shared/src/my-enum'
setTimeout(() => console.log(2 == MyEnum.B), 1000, 1)
my-enum.ts
export enum MyEnum{ A = 1, B, C }
When I build the back package typescript generates in the dist folder a test.js file with var my_enum_1 = require("#test/shared/src/my-enum");, however the compiled files of the shared package are in the dist folder, not in src, so if I try to run with node I get the following error:
Error: Qualified path resolution failed: we looked for the following paths, but none could be accessed.
Source path: E:\Repository\workspace_test\shared\src\my-enum
Not found: E:\Repository\workspace_test\shared\src\my-enum
Not found: E:\Repository\workspace_test\shared\src\my-enum.js
Not found: E:\Repository\workspace_test\shared\src\my-enum.json
Not found: E:\Repository\workspace_test\shared\src\my-enum.node
If I use ts-node instead of node it works, but that's not how I'd like to run this after deploy. I assume there's something I need to change in my tsconfig.json files but I can't find out what it is, other related SO questions I could find weren't very helpful.
I uploaded an example repo on github to make things easier to anyone willing to help, but here are my ts.config files:
Root:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true
}
}
Back:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "dist",
"rootDir": "src"
},
"references": [
{ "path": "../shared" }
]
}
Shared:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"composite": true,
"outDir": "dist",
"rootDir": "src"
}
}
Ok, so to fix this I had to change the package.json instead of the tsconfig.json. Making the main field in shared/package-json point to the dist folder doesn't do anything for some reason, but the exports field actually works, so creating an alias there solved the issue.
So for this example project it would be something like "exports": { "./src/my-enum": "./dist/src/my-enum.js" }.

Setting correct paths when configuring http server with npm and grunt for OpenUI5

I'm trying to setup an http server with node.js for using OpenUI5. My trials are based on the article found at
https://www.newventuresoftware.com/blog/developing-sapui5-applications-with-visual-studio-code
My problem is to correctly set the paths in the different files involved.
When executing
npm run start
in directory
/var/www/html/p1
the following error message appears in the browsers console:
2019-01-25 12:54:44 Device API logging initialized - DEVICE Device.js:85:22
2019-01-25 12:54:44.940000 Changing log level to DEBUG - sap.base.log Log.js:412:41
2019-01-25 12:54:44.940000 SAP Logger started. - Log.js:412:41
2019-01-25 12:54:44.941000 URL prefixes set to: - sap.ui.ModuleSystem Log.js:412:41
2019-01-25 12:54:44.941000 (default) : node_modules/#openui5/sap.ui.core/src/ - sap.ui.ModuleSystem Log.js:412:41
2019-01-25 12:54:44.941000 'myapp' : ./ - sap.ui.ModuleSystem Log.js:412:41
2019-01-25 12:54:45.333000 Creating Core - sap.ui.core.Core Log.js:412:41
2019-01-25 12:54:45.338000 theme = sap_belize - Log.js:412:41
2019-01-25 12:54:45.338000 modules = sap.m.library - Log.js:412:41
2019-01-25 12:54:45.338000 bindingSyntax = complex - Log.js:412:41
2019-01-25 12:54:45.338000 frameOptions = allow - Log.js:412:41
2019-01-25 12:54:45.338000 xx-supportedLanguages = - Log.js:412:41
2019-01-25 12:54:45.338000 xx-fiori2Adaptation = false - Log.js:412:41
2019-01-25 12:54:45.340000 Declared modules: sap.ui.core.library,sap.m.library - sap.ui.core.Core Log.js:412:41
2019-01-25 12:54:45.340000 Declared theme sap_belize - sap.ui.core.Core Log.js:412:41
2019-01-25 12:54:45.340000 Content direction set to 'ltr' - sap.ui.core.Core Log.js:412:41
2019-01-25 12:54:45.340000 Browser-Id: ff64 - sap.ui.core.Core Log.js:413:43
2019-01-25 12:54:45.341000 Sync point 'UI5 Document Ready' created - Log.js:412:41
2019-01-25 12:54:45.341000 Sync point 'UI5 Core Preloads and Bootstrap Script' created - Log.js:412:41
2019-01-25 12:54:45.342000 Sync point 'UI5 Core Preloads and Bootstrap Script' finished (tasks:2, open:0, failures:0) - Log.js:412:41
2019-01-25 12:54:45.343000 Modules and libraries declared via bootstrap-configuration are loaded synchronously. Set preload configuration to 'async' or switch to asynchronous bootstrap to prevent these requests. - SyncXHR Log.js:411:29
2019-01-25 12:54:45.347000 Analyzing Library sap.ui.core - sap.ui.core.Core.initLibrary() Log.js:413:43
2019-01-25 12:54:45.350000 Including node_modules/#openui5/sap.ui.core/src/sap/ui/core/themes/sap_belize/library.css - sap.ui.core.Core.includeLibraryTheme() - Log.js:412:41
Error: failed to load 'sap/m/library.js' from node_modules/#openui5/sap.ui.core/src/sap/m/library.js: 404 - Not Found
The file not found is located at
./node_modules/#openui5/sap.m/src/sap/m/library.js
directory tree:
/var/www/html/p1/
├── Gruntfile.js
├── index.html
├── node_modules
│   ├── abbrev
...
│   ├── #openui5
│   │   ├── sap.m
│   │   ├── sap.ui.core
│   │   ├── sap.ui.layout
│   │   ├── sap.ui.unified
│   │   └── themelib_sap_belize
...
│   └── yargs
│   ├── CHANGELOG.md
│   ├── completion.sh.hbs
│   ├── index.js
│   ├── lib
│   ├── LICENSE
│   ├── node_modules
│   ├── package.json
│   └── README.md
├── package.json
└── package-lock.json
404 directories, 1762 files
package.json:
{
"name": "P1",
"version": "1.0.0",
"private": true,
"description": "",
"dependencies": {
"#openui5/sap.m": "^1.60.0",
"#openui5/sap.ui.core": "^1.60.0",
"#openui5/sap.ui.layout": "^1.60.0",
"#openui5/themelib_sap_belize": "^1.60.0"
},
"devDependencies": {
"grunt": "^1.0.2",
"grunt-contrib-clean": "^1.1.0",
"grunt-contrib-connect": "^1.0.2",
"grunt-contrib-copy": "^1.0.0",
"grunt-openui5": "^0.13.0"
},
"scripts": {
"start": "grunt serve",
"build": "grunt build"
},
"author": "",
"license": "ISC"
}
Gruntfile.js:
'use strict';
module.exports = function(grunt) {
grunt.initConfig({
connect: {
options: {
port: 8080,
hostname: '*'
},
src: {},
dist: {}
},
openui5_connect: {
options: {
resources: [
'node_modules/#openui5',
],
testresources: [
'node_modules/#openui5',
]
},
src: {
options: {
appresources: '.'
}
},
dist: {
options: {
appresources: '.'
}
}
},
openui5_preload: {
component: {
options: {
resources: {
cwd: 'node_modules/#openui5',
prefix: 'myapp',
src: [
'**/*.js',
'**/*.fragment.html',
'**/*.fragment.json',
'**/*.fragment.xml',
'**/*.view.html',
'**/*.view.json',
'**/*.view.xml',
'**/*.properties',
'manifest.json',
'!test/**'
]
},
dest: 'dist'
},
components: true
}
},
clean: {
dist: 'dist',
coverage: 'coverage'
},
copy: {
dist: {
files: [ {
expand: true,
cwd: '.',
src: [
'**',
'!test/**'
],
dest: 'dist'
} ]
}
},
eslint: {
webapp: ['.']
}
});
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-openui5');
// Server task
grunt.registerTask('serve', function(target) {
grunt.task.run('openui5_connect:' + (target || 'src') + ':keepalive');
});
// Build task
grunt.registerTask('build', ['clean:dist', 'openui5_preload', 'copy']);
// Default task
grunt.registerTask('default', ['serve']);
};
index.html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<title>My Hello World App</title>
<script src="node_modules/#openui5/sap.ui.core/src/sap-ui-core.js"
id="sap-ui-bootstrap"
data-sap-ui-theme="sap_belize"
data-sap-ui-libs="sap.m"
data-sap-ui-compatVersion="edge"
data-sap-ui-resourceroots='{"myapp": "./"}'>
</script>
</head>
<body class="sapUiBody" id="content">
Hallo
</body>
</html>
What changes are necessary to make the server find the file
library.js
in the current directory layout?
Is there any hard reason that you are still using grunt and not the new UI5 Build Tooling?
Just take a look at the Openui5 Sample App how to use it.
In your case you should put the index.html into the webapp folder, set the the bootstrapping to
<script src="resources/sap-ui-core.js"
and use the following package.json
{
"name": "P1",
"version": "1.0.0",
"private": true,
"description": "",
"dependencies": {
"#openui5/sap.m": "^1.60.0",
"#openui5/sap.ui.core": "^1.60.0",
"#openui5/sap.ui.layout": "^1.60.0",
"#openui5/themelib_sap_belize": "^1.60.0"
},
"devDependencies": {
"#ui5/cli": "^1.0.0",
"eslint": "^4.19.1",
"karma": "^3.1.3",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.1.2",
"karma-openui5": "^0.2.3",
"karma-qunit": "^1.2.1",
"qunitjs": "^2.4.1",
"rimraf": "^2.6.2",
"start-server-and-test": "^1.4.1"
},
"scripts": {
"start": "ui5 serve",
"lint": "eslint webapp",
"karma": "karma start",
"karma-ci": "karma start karma-ci.conf.js",
"watch": "start-server-and-test start http://localhost:8080 karma",
"test": "npm run lint && rimraf coverage && start-server-and-test start http://localhost:8080 karma-ci",
"build": "rimraf dist && ui5 build --a"
},
"author": "",
"license": "ISC"
}
then run npm install and after that ui5 init once.
After that start your app with npm start
If you really want to stick with grunt, checkout the
legacy-grunt-and-bower-setup branch of the Openui5 Sample App and use the Gruntfile/bower.json/package.json from here.

Private github repo as dependency is extraneous on npm install

This is my first time using a private repo as a dependency in another project. I think I am doing it right, but the dependency is not available in the new project after install and is not in node_modules.
Following this post I can see that I am including it in the package.json correctly like:
"myPackage": "git+https://github.com/myusername/mygitrepository.git"
When I run npm install on this package, I see this that it does not have an error, but after this dependency in the list, it is shown with extraneous(git+https://github.com/myusername/mygitrepository.git).
Even though there is the extraneous issue, there is no error, and the dependency is not available or listed in node_modules.
Update: repo_A package.json
{
"name": "project-name",
"version": "1.0.0",
"description": "Backend utility functions",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/user/repo.git"
},
"author": "me",
"license": "ISC",
"bugs": {
"url": "https://github.com/user/repo/issues"
},
"homepage": "https://github.com/user/repo#readme",
"dependencies": {
"lodash": "^4.17.4",
"mongodb": "^2.2.25",
"redis": "^2.7.1",
"winston": "^2.3.1"
}
}
Update: repo_B package.json
{
"name": "main-project-name",
"version": "1.0.0",
"description": "",
"repository": {
"type": "git",
"url": "git+https://github.com/user/repo.git"
},
"author": "someone else",
"homepage": "https://github.com/user/repo.git",
"dependencies": {
"async": "2.1.4",
"chai": "^3.5.0",
"langs": "1.1.0",
"lodash": "4.13.1",
"node-stopwatch": "0.0.1",
"request": "2.74.0",
"winston-loggly": "^1.3.1",
"utils": "user/repo_A.git#master"
},
"scripts": {
"test": "mocha"
}
}
Update for latest steps
Here are the steps I am following now to test each possible solution, followed by the output:
rm -rf node_modules
npm cache clean
npm install
Output
├─┬ async#2.1.4
│ └── lodash#4.17.4
├─┬ chai#3.5.0
│ ├── assertion-error#1.0.2
│ ├─┬ deep-eql#0.1.3
│ │ └── type-detect#0.1.1
│ └── type-detect#1.0.0
├── util#1.0.0 extraneous (git+ssh://git#github.com/user/repo_A.git#commit-ish)
.......
If you specify https then that will be looking for a login user and password I believe, which I don't think it can load automatically. I would list it simply as "user/repo" and make sure that machine has an ssh key on it that is in github like the setup described in help such as https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/#platform-linux and that things are setup so that pulling down that repo does not require user interaction.
EDIT: After testing, I think the issue is that your name in the package.json does not match how you have listed it in your main project's dependencies. In my test, this resulted in the modules being installed but I got the extraneous message.

Resources