Help with the VSCode formatter?

Hello hello! Just getting started with ReScript and I’m running into trouble with VSCode.

Everything seems to work great except for some reason I can’t get “format on save” to work.

If I manually run ./node_modules/.bin/rescript format it works.

I’ve tried a bunch of different things in VSCode settings - variations on disabling/uninstalling Prettier and setting ReScript as the default formatter in workspace settings vs settings.json. Anyone run into this?

Hello and welcome to the wonderful world of ReScript!

I am assuming ReScript 12 and the most current stable rescript-vscode plugin version ( 1.70.0 as of today)?

Did you try formatting in a vanilla project?

npm create rescript-app@latest

Because I just tried and it worked fine to format the .res files in there. Usually formatting only fails for me if there is an parse error in the same file.

Please tell us more about your setup which is probably more complex than the basic example I suggested to try formatting on?

2 Likes

Thanks Florian!

So it is using v12, I’m on v1.70.0 of the plugin, and I did create the project using:

npm create rescript-app@latest.

I just tried a couple fresh installs using both the “Vite” and “Basic” templates. I got the same result: “format on save” doesn’t work, but running:

./node_modules/.bin/rescript format

… works just fine!

Here’s everything relating to ReScript in my vscode default settings:

{
	// Defines a default folding range provider that takes precedence over all other folding range providers. Must be the identifier of an extension contributing a folding range provider.
	//  - chenglou92.rescript-vscode: ReScript language support (official)
	// Defines a default notebook formatter which takes precedence over all other formatter settings. Must be the identifier of an extension contributing a formatter.
	//  - chenglou92.rescript-vscode: ReScript language support (official)
	// ReScript
	// Whether you want the extension to prompt for autostarting a ReScript build if a project is opened with no build running.
	"rescript.settings.askToStartBuild": true,

	// Path to the directory where cross-platform ReScript binaries are. You can use it if you haven't or don't want to use the installed ReScript from node_modules in your project.
	"rescript.settings.binaryPath": null,

	// Enable project config caching. Can speed up latency dramatically.
	"rescript.settings.cache.projectConfig.enable": true,

	// Enable (experimental) code lens for function definitions.
	"rescript.settings.codeLens": false,

	// Show compile status in the status bar (compiling/errors/warnings/success).
	"rescript.settings.compileStatus.enable": true,

	// (beta/experimental) Enable incremental type checking across files, so that unsaved file A gets access to unsaved file B.
	"rescript.settings.incrementalTypechecking.acrossFiles": false,

	// (debug) Enable debug logging (ends up in the extension output).
	"rescript.settings.incrementalTypechecking.debugLogging": false,

	// Enable incremental type checking.
	"rescript.settings.incrementalTypechecking.enable": true,

	// Enable (experimental) inlay hints.
	"rescript.settings.inlayHints.enable": false,

	// Maximum length of character for inlay hints. Set to null to have an unlimited length. Inlay hints that exceed the maximum length will not be shown.
	"rescript.settings.inlayHints.maxLength": 25,

	// Path to the directory where platform-specific ReScript binaries are. You can use it if you haven't or don't want to use the installed ReScript from node_modules in your project.
	"rescript.settings.platformPath": null,

	// Optional path to the directory containing the @rescript/runtime package. Set this if your tooling is unable to automatically locate the package in your project.
	"rescript.settings.runtimePath": null,

	// Enable signature help for function calls.
	"rescript.settings.signatureHelp.enabled": true,

	// Enable signature help for variant constructor payloads.
	"rescript.settings.signatureHelp.forConstructorPayloads": true,
}

And then here is what I have in the workspace settings:

{
  "editor.defaultFormatter": "chenglou92.rescript-vscode",
  "editor.formatOnSave": true
}

Not sure if there are any clues in there? If that all looks ok then I must have some other conflict somewhere.

~Eileen

Weird. Can you check the output of “Command Palette (CMD-Shift-P)” → "DEBUG ReScript: Dump LSP Server State "

Especially the bscBinaryLocation, e.g.:

"bscBinaryLocation": "/Users/florian/stuff/test-rescript-12/node_modules/@rescript/darwin-arm64/bin/bsc.exe",

If that is somehow not set try to set the platform path in the workspace settings:

"rescript.settings.platformPath": "node_modules/@rescript/darwin-arm64/bin",

Adjusted for the OS and platform you use:

  • @rescript/darwin-arm64
  • @rescript/darwin-x64
  • @rescript/linux-arm64
  • @rescript/linux-x64
  • @rescript/win32-x64

This looks ok to me :thinking:

Editing the platform path also didn’t seem to have any effect.

{
  "config": {
    "askToStartBuild": false,
    "inlayHints": {
      "enable": true,
      "maxLength": 25
    },
    "codeLens": true,
    "signatureHelp": {
      "enabled": true,
      "forConstructorPayloads": true
    },
    "incrementalTypechecking": {
      "enable": true,
      "acrossFiles": true,
      "debugLogging": true
    },
    "cache": {
      "projectConfig": {
        "enable": true
      }
    },
    "binaryPath": null,
    "platformPath": null,
    "runtimePath": "",
    "compileStatus": {
      "enable": true
    },
    "allowBuiltInFormatter": true,
    "autoRunCodeAnalysis": true
  },
  "projects": [
    {
      "projectRootPath": "/Users/eileennoonan/Code/arcstone/insi/my-rescript-app",
      "openFiles": [
        "/Users/eileennoonan/Code/arcstone/insi/my-rescript-app/src/App.res"
      ],
      "filesWithDiagnostics": [],
      "filesDiagnostics": {},
      "rescriptVersion": "12.0.1",
      "bscBinaryLocation": "/Users/eileennoonan/Code/arcstone/insi/my-rescript-app/node_modules/@rescript/darwin-arm64/bin/bsc.exe",
      "editorAnalysisLocation": "/Users/eileennoonan/Code/arcstone/insi/my-rescript-app/node_modules/@rescript/darwin-arm64/bin/rescript-editor-analysis.exe",
      "namespaceName": "",
      "hasPromptedToStartBuild": false,
      "bsbWatcherByEditor": null
    }
  ],
  "workspaceFolders": [
    "/Users/eileennoonan/Code/arcstone/insi/my-rescript-app"
  ]
}

Just curious, but does a regular (manual) invocation of “Format Document” work for you?

Oooh!

First it told me that the ReScript formatter wasn’t installed. Then it gave me an option to install it. I click “install” and then I got a message saying the ReScript formatter crashed 5 times in the last 3 minutes, and gave me some error output.

I’m unclear what this “version” is …

TypeError: Invalid version. Must be a string. Got type "undefined".
    at new e (/Users/eileennoonan/.vscode/extensions/chenglou92.rescript-vscode-1.70.0/server/out/cli.js:37:4740)
    at OR (/Users/eileennoonan/.vscode/extensions/chenglou92.rescript-vscode-1.70.0/server/out/cli.js:37:10086)
    at Object.GR [as gt] (/Users/eileennoonan/.vscode/extensions/chenglou92.rescript-vscode-1.70.0/server/out/cli.js:37:10689)
    at Rt (/Users/eileennoonan/.vscode/extensions/chenglou92.rescript-vscode-1.70.0/server/out/cli.js:40:7856)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async lt (/Users/eileennoonan/.vscode/extensions/chenglou92.rescript-vscode-1.70.0/server/out/cli.js:40:8463)
    at async Jw (/Users/eileennoonan/.vscode/extensions/chenglou92.rescript-vscode-1.70.0/server/out/cli.js:46:10624)
    at async process.wy (/Users/eileennoonan/.vscode/extensions/chenglou92.rescript-vscode-1.70.0/server/out/cli.js:46:17558)

Node.js v22.20.0
[Error - 10:33:22 AM] The ReScript Language Server server crashed 5 times in the last 3 minutes. The server will not be restarted. See the output for more information.

This seems to me your real problem.
I would assume a lot of the tooling isn’t working for you.
You should not need to install anything special (besides the vscode extension) to format.
Because the LSP failed, you got that suggestion to install something because nothing is responding to the format request.

Can you enable "rescript.settings.incrementalTypechecking.debugLogging": true in your workspace and check the Output pane.
Open a ReScript file and type something, then ReScript Language Server should show you some more info.
(You may want to restart it first via vscode command)

1 Like

Yeah I’m not sure at what point I lost autocomplete. It had been working, but I messed around quite a bit in the settings.

In any case, I just nuked my entire VSCode install and reinstalled it and now everything works perfectly :pinched_fingers:

3 Likes