how to include a file in module rust in vim - rust

Whenever I create a new rust file in neovim it says that it is not a part of module tree and I get a bunch of errors. The weird thing is it eventually it just starts working but that is not good enough so my question is how to manually add files to the module tree in vim.
I have tried restarting vim and reinstalling coc rust but nothing has worked.

For files to be part of the module tree they have to be mentioned in the hierarchy somewhere i.e.:
// my_mod.rs
pub fn some_code() {}
// lib.rs or main.rs
mod my_mod; // without this line you will get the error
If the file is mentioned and the error still persists sometimes the state in rust_analyzer gets out of sync and a restart of it should resolve it.

Related

More issues with TypeScript modules

... yep. It's that time of year again. TypeScript is wonderful, except for the mess that is the module system. Presently, I'm struggling with a scenario where I'm dealing with two separate repositories / projects (what ever you wanna call it). I've got a library and a program using said library. I'm using Node's subpath exports feature in the package.json to indicate multiple submodules within the library. From the NodeJS (and the bundler's perspective) this is fine, but not in TypeScript's world. TypeScript is adamant that none of the modules exist.
Here's a quick summary of the library
/utils
package.json
tsconfig.json
build automatically generated build directory
iter.js
iterSync.js
...
lib autogenerated declarations files
iter.d.ts
iterSync.d.ts
...
src source files
iter.ts
iter_sync.ts
...
lib manually written definitions files
iter.d.ts
iterSync.d.ts
It's package.json has an exports field which defines a list of exports and their respective declaration file.
library/package.json
...
"exports": {
"./iter": "build/iter.js",
"./iterSync": "build/iterSync.js",
...
}
...
I've tried various alterations of the above, such as
Node16 module resolution strategy
Adding a types property to each export
Using the main and typings fields in library/package.json
Adding "lib" to the includes of library/tsconfig.json
Upgrading to TypeScript 4.6.4
Restarting the TS Language Server through the VSCode command menu
Basically all permutations of the above
How I use the library
I don't think it makes a difference, but I'm referencing the module through a pnpm link dependency statement for development, and a git://... url for production:
program/package.json
...
"dependencies": {
"jcake-utils": "link:../library", // dev
"jcake-utils": "github:J-Cake/jcake-utils", // prod
...
}
Regardless of what I do, TypeScript refuses to locate my submodules in the program
...
import Iter, * as iter from 'jcake-utils/iter';
import IterSync, * as iterSync from 'jcake-utils/iterSync';
...
$ pnpm exec tsc -v
Version 4.6.4
$ pnpm exec tsc -p program/tsconfig.json
src/build.ts:2:29 - error TS2307: Cannot find module 'jcake-utils/iter' or its corresponding type declarations.
... (A bajillion errors saying exactly the same thing, just across different files)
I alluded to this being a common issue (because from my experience it is) but I would greatly appreciate some very specific feedback as to how I can correct this issue and/or if I should potentially restructure either program or library, and how best to do so. I've been unable to find documentation that is helpful in my situation, as many of the suggestions given by the TypeScript community have not worked.
Thanks kindly

Ignore filename extension for child_process fork using node-dev and Typescript

I have a project written in Typescript, and I'm using node-dev alongside ts-node in my local enviroment for development. I'm using child_process's fork method to instantiate a subprocess, like so:
fork(path.join(__dirname, './worker.ts'));
This works fine, and I can even set breakpoints in VS Code for the worker.
The problem is that when building (transpiling) my project, a MODULE_NOT_FOUND exception is thrown because worker.ts turned into worker.js. Initially, mi idea was to omit the file extension when forking (fork(path.join(__dirname, './worker'));), but if I do so, when running the project with node-dev, it throws a MODULE_NOT_FOUND because it can't resolve the file if the extension is not present.
Is there any workaround for this? Maybe an extra configuration option for node-dev?
I'm on Windows 10 using node v12.22.1
A simple solution I suggest would be to read in the current file type. The compiled files should end with ".js" while the source files end with ".ts". Using the path.extname methode in combination with __filename you can easily extract that file extension and simply concat it to your file name.
fork(path.join(__dirname, './worker' + path.extname(__filename)));

Typescript variable syntax error on terminal

i try to learn TypeScript on Visual Studio Code. But i get an sim error that i don't understand beacuse of defining variable. I'm define as that:
let year:number = 2015
I get a syntax error on terminal beacuse i'm put a colon and write the variable type after the variable name.
How can i correct this error?
As the question is quite old,, but I am answering for other developers like me who stuck with same issue. Enhancement in #basarat's answer:
You need to
Compile .ts file
&
Run .js file
Example: You have created a typescript file HelloWorld.ts
When you want to compile do
tsc HelloWorld.ts
.js file will be generate. Then to run your code do the following
node HelloWorld.js
Voila!!
How can i correct this error?
Don't run .ts files directly.
Solution
Compile to js before running. e.g.
tsc srcfiles
More
Quickstart : https://basarat.gitbook.io/typescript/content/docs/quick/nodejs.html

How do I configure SublimeLinter-contrib-rustc to find the "piston" crate?

I'm building a Rust game with Piston and I'm trying to use the SublimeLinter Rust package. When I open my .rs game files, I get this linter error:
extern crate piston; // linter error: "can't find crate for 'piston'"
If I check the Sublime console, I can see that the linter is finding rustc:
SublimeLinter: rust activated: /usr/local/bin/rustc
I don't see any obvious error messages in the console. I have piston listed as a dependency in my Cargo.toml and I'm able to cargo run successfully.
I'm a complete Rust & SublimeLinter newbie.
I get a similar error for whichever import I put first; for example if I move extern crate graphics; to be the first line in the file I get the error "can't find crate for 'graphics'".
EDIT: turns out enabling the "use-cargo" setting fixed the issue. I added a ".sublimelinterrc" file to my project root with these contents:
{
"linters": {
"rust": {
"use-crate-root": true,
"use-cargo": true
}
}
}
I have not been able to reproduce your error. However, I'll list what I did that seemed to work. Perhaps you will see a step where we diverged, and that will give you a hint of where to look.
I installed Sublime Text 3. I chose the stable channel, build 3083. I installed Package Control, and then three packages:
SublimeLinter (3.5.1)
Sublime​Linter-contrib-rustc (1.3.5)
Rust (2015.05.28.16.43.21)
I restarted Sublime Text and opened a project of mine that uses Cargo and depends on other packages. I opened the console (ctrl-`) and enabled debug mode, which takes effect at the next Sublime restart.
Because I have non-standard install of Rust, I saw that Sublime was unable to run rustc due to missing dynamic libraries. I then closed Sublime, and opened it from my terminal: open /Applications/Sublime\ Text.app/Contents/MacOS/Sublime\ Text. This uses my environment variables set by my shell.
I saw that the linter was complaining that my library had no main method, so I enabled the use-cargo option.
SublimeLinter: rust output:
error: main function not found
error: aborting due to previous error
After changing the use-cargo setting, I caused a deliberate syntax error in my code and saw that it was highlighted. I then misnamed the crate in the extern crate line, and saw it was highlighted.
At no point did there seem to be a problem with finding crates. I could tell that cargo was being used via the console:
SublimeLinter: rust output:
Updating registry `https://github.com/rust-lang/crates.io-index`
Downloading rand v0.3.9
...

Jade: "TypeError: path must be a string" with --watch, when saving .jade file

I have jade watching my directory to auto compile the templates to html files.
I've entered the following into the command line: jade --watch templates.
This returns rendered templates/index.html and compiles the .jade file just fine at first.
But when I attempt to save the .jade file again, it does not compile, and returns this error in the command line:
TypeError: path must be a string
at Object.fs.lstat (fs.js:675:11)
at renderFile (/usr/local/lib/node_modules/jade/bin/jade.js:172:6)
at StatWatcher.<anonymous> (/usr/local/lib/node_modules/jade/bin/jade.js:119:11)
Not sure what "path" it's referring to. But in case it's the directory "templates", I've tried the same command with the directory in quotes.
This is apparently only an issue with the current jade version, 1.8.2. If you lock your dependency in your package.json to the previous version (like below), this issue goes away.
{
"name": "my-app",
"dependencies": {
"jade": "1.8.1"
}
}
I've gone ahead and submitted a new github issue, so hopefully this will get resolved.
Ive run into the same problem and unfortunately ive run into a real wall on finding any documentation on this
If you want to, i found an alternative which i find adequate:
Download the code from here: http://swarminglogic.com/jotting/2014_02_watchfile
Its a simple file watcher
Alias the code or whatever your preference is.
Run watchfile test.jade jade ./ --out /tmp in the right directory. This will run a jade compilation through watchfile every time your file changes.
Workaround i know but it works... Why does people always take for granted that you bundle everything when running node services? On searches for jade html TypeError: path must be a string i only come upon Express.js threads

Resources