How can I configure the 'staticcheck' linter in Visual Studio Code? - go

I installed staticcheck, but it doesn't tell me any problems in my Visual Studio Code.
I configured my linter to use staticcheck and I looked everywhere on the Internet. It still doesn't work.
Here is a part of my settings.json file:
{
"go.formatTool": "goimports",
"go.useLanguageServer": true,
"go.lintTool": "staticcheck",
"go.lintFlags": [],
"go.testFlags": ["-v"],
"go.toolsManagement.autoUpdate": true,
"editor.codeActionsOnSave": { "source.fixAll.eslint": true }
}
I already tried to add "-check=all" on the go.lintFlags. I reloaded my Visual Studio Code, but it still doesn't work.
When I checked the official website they talk about staticcheck.conf, but I don't get it since there are multiple files named staticcheck.conf on my system.

You don't need "go.lintTool": "staticcheck", because, this is (staticcheck) default linting tool in vscode-go.
If you have language serve enabled, you need to turn staticcheck explicitly with
"gopls": { "ui.diagnostic.staticcheck": true }.
As alternative, you can set up golangci-lint as your linter and turn staticcheck in it.

Related

How is VSCode finding this Go linting Problem and how do I ignore it?

I'm setting up linting with golangci-lint in my Go project. I have a file generated by go-bindata that VSCode is listing the following under the Problems tab:
assets/assets.go: redundant type from array, slice, or map composite literal (simplifycompositelit)
I can't seem to get rid of it. It's not a compiler error and I'll be re-running go-bindata from time to time so I don't want to make a habit of modifying generated code.
Right now, with the configuration below, I can't make VSCode stop reporting this error. If I run golangci-lint run ./... in the root of the workspace I get no output. I can provide my linting config if needed but VSCode seems to be running something else. How do I figure out what's reporting this error and how do I make it stop reporting anything for the file assets/assets.go in this one workspace?
Here's Go-related vscode settings:
{
"go.formatTool": "gofmt",
"go.lintTool": "golangci-lint",
"go.liveErrors": {
"enabled": true,
"delay": 500
},
"go.lintOnSave": "workspace",
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"go.useLanguageServer": true,
"go.languageServerExperimentalFeatures": {
"diagnostics": true,
"documentLink": true
},
}
Here's the line in question even with a nolint comment to show it's not behaving as expected. If it were golangci-lint outputting this, the nolint would prevent the warning from showing. I reloaded the window and closed/reopened vscode to be sure the change was noticed.
After reproducing locally, it seems this message comes from gopls, as disabling gopls silences the message. There are a couple of related complaints/issues on the Go issue tracker:
hide gofmt -s diagnostics (and others?) in generated files
should not issue lint-style warnings on generated code
Neither offers an actual solution.
However, this issue on the vscode-go repo, provides a work-around. In your VSCode config, add the gopls.analyses.simplifycompositelit key, with a value of false:
"gopls": {
"analyses": {
"simplifycompositelit": false
},
}
Of course, this disables it for all projects, not just generated files, but if you're also using golangci-lint, it can be configured to catch the same types of errors, and can be configured on a more granular basis, so that you won't miss the same class of errors in non-generated code.

vscode( vscode-ruby + rubocop ) how to auto correct on save?

Environments
vscode Version 1.19.1 (1.19.1)
rubocop (0.52.1)
Darwin mbp 16.7.0 Darwin Kernel Version 16.7.0: Wed Oct 4 00:17:00 PDT 2017; root:xnu-3789.71.6~1/RELEASE_X86_64 x86_64
ruby 2.3.5p376 (2017-09-14 revision 59905) [x86_64-darwin16]
followed https://github.com/rubyide/vscode-ruby#linters and installed all gems and edited the settings.json like this.
{
"ruby.rubocop.executePath": "/Users/ac/.rbenv/shims/",
"ruby.rubocop.onSave": true,
"ruby.lint": {
"ruby": {
"unicode": true //Runs ruby -wc -Ku
},
"reek": true,
"rubocop": {
"lint": true,
"rails": true
},
"fasterer": true,
"debride": {
"rails": true //Add some rails call conversions.
},
"ruby-lint": true
},
"ruby.locate": {
"include": "**/*.rb",
"exclude": "{**/#(test|spec|tmp|.*),**/#(test|spec|tmp|.*)/**,**/*_spec.rb}"
}
}
On vscode, code highlighting is working fine.
*just to note, you see the extensions installed, and warnings in the problem tab.
Question
I was under the inpression that vscode-ruby and rubocop would auto-correct indentations and cop rules on file save, but apparently it doesn't.
If I want it to format my code like prettier, how should I set this up?
Per this comment on the vscode-ruby-rubocop GitHub, you can use the following settings:
{
"editor.formatOnSave": true,
"editor.formatOnSaveTimeout": 5000,
"ruby.rubocop.executePath": "path/where/rubocop/is/located",
"ruby.format": "rubocop",
}
Just applied them to my user settings on my local box and it appears to work. VS Code was throwing an error for my ruby.rubocop.executePath setting saying it wasn't executable, and removing the line appears to not cause that error to show, and still formats my code accordingly. Setting a lower timeout (I tried 2500) also seems to break auto format on saving, so I'd suggest leaving it at 5000.
I've tried all the options other people shared and it fixed linking for me (so I would get warnings when my code wasn't formatted correctly) but it didn't fix indentation or other formatting on save.
What seemed to work for me is adding a default formatter option. If you look at the bottom right corner in vscode you should see a notification icon which might throw in a few warnings that can help with your config. For me it was adding:
"[ruby]": {
"editor.defaultFormatter": "misogi.ruby-rubocop"
}
Now, it's enough just adding these lines:
{
"ruby.rubocop.onSave": true,
"editor.formatOnSave": true,
}
I had this problem for a while and none of the other solutions worked for me.
According to a github comment, what solved was:
I Replace bin in the PATH with wrappers and solved the issue:
vscode setting:
ruby.rubocop.executePath": "/Users/USER_NAME/.rvm/gems/ruby-2.6.5/wrappers/
Note: Another useful thing you could check if none of the above worked for you is to check your home folder for a .rubocop.yml file (~/.rubocop.yml) and delete it so you only have your project's .rubocop.yml working on.
To make sure autocorrection works with ruby-rubocop add below settings
make sure "ruby.rubocop.executePath": "path/where/rubocop/is/located", is set to default
"ruby.rubocop.executePath": "",
Add the following to your json file in vscode
"editor.formatOnSave": true,
"editor.formatOnSaveTimeout": 5000,
"ruby.rubocop.executePath": "",
"ruby.format": "rubocop",
I struggled with the extension ruby-rubocop, last updated in Dec 2021. It seems it's still using the auto-correct command --autocorrect while Rubocop now uses --auto-correct. I switched to another extension, ruby-rubocop-revived, which fixed my auto-correct issue.
It also introduces the useServer option that speeds up the execution.
Here are the settings I used:
# .vscode/settings.json
"ruby.format": "rubocop",
"ruby.rubocop.onSave": true,
"ruby.rubocop.useServer": true,
"[ruby]": {
"editor.defaultFormatter": "rebornix.ruby"
},
This comment is now thankfully outdated
--
Unfortunately this cannot be done with the current rubocop extension. The primary use case is to lint your ruby and show visual cues in the IDE.
It is currently an open issue/feature request on github. View this issue to see progress until it is resolved.
https://github.com/misogi/vscode-ruby-rubocop/issues/49

Can't set TypeScriptExperimentalDecorators to True in TypeScript 1.8.5

I downloaded a project from codeproject that demonstrates a simple working angular 2 application. I unblocked it, loaded it into Visual Studio 2013, and was able to successfully execute it as it.
I then cleaned the application and rebuilt it, and the application no compiles. It outputs the error: "Build: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning."
Under Visual Studio 2013, you currently need to edit the csproj file and add the entry
<TypeScriptExperimentalDecorators>true</TypeScriptExperimentalDecorators>
to get it to work. But under TypeScript 1.8.5 this doesn't appear to do anything.
Here's the app:
First Angular2 App with TypeScript and Visual Studio 2013
I know that I can go into the TypeScript build tab and untick the box "Do not emit outputs if any errors are reported" and it will generate the code and run successfully, but this should be prevented for real errors, not this one.
Any ideas how I might get this to work in VS2013? (I should point out, I did the same thing in VS2015 and got the same failed outcode.)
I was able to resolve this error by setting the experimentalDecorators compiler option to true in tsconfig.json.
Here I have modified an example tsconfig.json file to include this option.
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"outFile": "../../built/local/tsc.js",
"sourceMap": true,
"experimentalDecorators": true
},
"files": [
"core.ts",
"sys.ts",
"types.ts",
"scanner.ts",
"parser.ts",
"utilities.ts",
"binder.ts",
"checker.ts",
"emitter.ts",
"program.ts",
"commandLineParser.ts",
"tsc.ts",
"diagnosticInformationMap.generated.ts"
]
}

How to set vscode format golang code on save?

I'm using vscode with Go extensions to edit golang source code. Each time I want to format code, I have to press Ctrl-Shift-i on linux, Shift-Alt-F on Windows, or Ctrl-Shift-p and type format. Is it possible to set format on save, that is, when I press ctrl-s, it format the code automatically using go.fmt (or something alike)?
From my visual code version, i cannot use config go.formatOnSave": false.
Then I can turn them off in settings as below:
Build (Turn off using go.buildOnSave setting)
Lint (Turn off using go.lintOnSave setting)
Vet (Turn off using go.vetOnSave setting)
Format (Turn off by adding the below in your settings):
"[go]": {
"editor.formatOnSave": false
}
You should install this plugin: https://github.com/golang/vscode-go. One of the options is to set "auto format" on save: go.formatOnSave": false. It uses the Golang tooling for formatting.
For me, none of the answers worked. My Go version is 1.17.1, VSCode version is 1.60.1 and I'm using Linux Pop!_os.
After some digging online found this in the official VSCode documentation for Go. https://code.visualstudio.com/docs/languages/go#_formatting
My settings.json looks like this
"[go]": {
"editor.insertSpaces": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "golang.go"
},
Note: You need to install the required extensions for the go lang in VS code. Check the bottom left bar after opening a *.go file and you should see the go version. If you see an exclamation icon click on it and install the suggested extensions.
For me the followed settings worked. I disabled the annoying import refactoring.
"[go]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": false
},
}

How to debug Typescript in Visual Studio 2015 asp.net 5?

Is there a way to debug typescript files from visual studio 2015 CTP6 asp.net 5 application?
If not then maybe there is a way to configure source map files, so when I debug them in browser and can automatically save changes to my .ts files on server?
This might be hard, because .ts files are compiled by gulp, but maybe somebody found good solution for that?
Debugging TypeScript with Visual Studio works with the right settings.
First you make sure that you create source maps when compiling TypeScript to JavaScript. So you should have an xxx.js.map file near every xxx.js.
Getting source maps by running the TypeScript compiler outside of Visual Studio does not cause any difficulty, at the tsc command line add:
--sourcemap %1.ts
your gulp script will usually create sourcemaps by default.
Configure your web application in Visual Studio.
Set Internet Explorer as the start browser. I got it working only with IE and don't think any other browser will work. (edit: Somewhere I read that there is a Webkit debugging bridge, so Chrome debugging should be possible, but I do not remember the source.)
In the project properties go to the "Web" tab and configure the "Debuggers" section at the bottom: Disable all debuggers! This is counter intutitive and you might see this error message:
You have attempted to start the debugger, but based on your current debug settings on the Web properties page there is no process to debug.
This occurs when the "Don't open a page. Wait for a request from another process" option is selected, and ASP.NET debugging is disabled. Please check your settings on the Web properties page and try again. As the error message says, the Start Action at the top of the Web properties should be another option, like "Current page".
Set breakpoints in your .ts code inside Visual Studio now or later.
Hit F5
While you can use the Visual Studio Editor to debug and edit .ts files, "Edit and Continue" will not work, there is currently no browser that can reload .js and .js.map files and continue. (Correct me anyone if I am wrong and I will be happy.)
The best way to handle map files I found is to include the source inside the generated .js files themselves. This is obviously a solution for a dev environment only.
my tsconfig.json below:
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"target": "es5",
"module": "system",
"moduleResolution": "node",
"outDir": "./wwwroot/scripts",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"inlineSourceMap": true,
"inlineSources": true
},
"exclude": [
"node_modules",
"wwwroot"
]
}
The settings you are after are inlineSourceMap and inlineSources.
This also helps with issues caused by moving the generated .js to different paths and not having to worry about where the map files go.
In ASP.NET 5 RC1 you can instruct the TypeScript compiler to generate .map files by adding a file called tsconfig.json to the project root directory. Visual Studio even has a template for this called "TypeScript JSON Configuration File". By default it has the following contents:
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5"
},
"exclude": [
"node_modules",
"wwwroot"
]
}
The "sourceMap" setting instructs the TypeScript compiler to generate the map files which are needed for debugging.
In my case I removed the "wwwroot" value from the "exclude" section because my Angular TypeScript source files are located under wwwroot\App.
Detailed explanation of tsconfig.json settings can be found here: https://github.com/Microsoft/typescript/wiki/tsconfig.json
When you are using gulp you can write the sourcemaps with a relative directory. This way your TS files are correctly resolved by Visual Studio. I use gulp-typescript and gulp-sourcemaps for my TypeScript compilation.
Below the compilation part of my gulpfile.js file (remember to disable the TypeScript compilation in your project file)
var tsProject = ts.createProject({
declarationFiles: false,
noExternalResolve: false,
target: 'ES5',
sortOutput: true,
noEmitOnError: false,
sourceMap:true
});
gulp.task('script', function () {
var tsResult = gulp.src('App/Ts/**/*.ts')
.pipe(sourcemaps.init())
.pipe(ts(tsProject));
return tsResult.js.pipe(concat('App.js'))
.pipe(sourcemaps.write('.', {includeContent:false, sourceRoot: '../../App/Ts/'}))
.pipe(gulp.dest('wwwroot/js'));
});
(I modified my gulpfile for readability, so check for any compilation / coding errors)
The simplest way I found is to put all TypeScript files in the wwwroot/app folder and leave the generated js files by Visual Studio to go where they do by default. Using gulp I simply inject them into HTML files. In Visual Studio 2015 it works great, and with gulp tasks I can also easily configure a task to bundle js for production.

Resources