question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extension Development: ensure package.json contributions match the implementation

See original GitHub issue

When developing extensions for VS Code, a lot of string keys are used. These keys are usually defined in the contribution section of a package.json and must match the keys used in the code to register the implementation. It is very easy to rename one part and forgetting the other.

Example

package.json

...
"activationEvents": [
	"onCustomEditor:hediet.vscode-drawio"
],
"contributes": {
	"customEditors": [
		{
			"viewType": "hediet.vscode-drawio",
			...
		},
	],
}

extension.ts

vscode.window.registerCustomEditorProvider2(
	"hediet.vscode-drawio",
	new DrawioEditorProvider(server),
	{
		supportsMultipleEditorsPerDocument: false,
		webviewOptions: { retainContextWhenHidden: true },
	}
)

Problem

All hediet.vscode-drawio strings have to match. This is also the case when declaring config properties. I managed multiple times now to rename one or two of them and forgetting about the others. This caused preventable bugs.

Solution

It would be really great if this could be improved. I have two ideas for that:

  1. Implementing lint rules that check that those keys align. This would include a check that all viewTypes used in the code must be mentioned in the package.json.
  2. Specifying all contributions with typescript and updating package.json automatically on each build.

The last idea could look like this: contributions.ts

const myCustomEditorViewTypeId = new vscode.ViewTypeId("hediet.vscode-drawio");
const myConfigProperty = new vscode.ConfigProperty<string>("hediet.vscode-drawio.my-property", { type: "string" });
export const contributions = new vscode.Contributions({   
   customEditors: [{ viewTypeId: myCustomEditorViewTypeId }],
   configurations: [{ property: myConfigProperty }]
});

Discussion

I really like 2) since it would also enable type safe config properties, go to definition and other features provided by the great tooling around TypeScript. However, I don’t see good ways to automatically update/generate a package.json on each build.

What do you think?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:12
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
connor4312commented, May 16, 2020

I’d be interested in something in this space. There are many places where constants are shared between the contributions and registrations in the extension. For the js-debugger I made an ad-hoc script to generate parts of my contributions, where my commands, debugger options, and localized strings are typed checked nicely.

I think that a good first step for exploration would be some vscode-typed-contributions package that extension authors could grab and mix into their build process. In js-debug I made type-guard functions for VS Code APIs, but I think with some cleverness you could have a drop-in replacement package that adds strong typings to the API in a forwards-compatible way.

1reaction
vscode-triage-botcommented, Jul 13, 2020

🙁 In the last 60 days, this feature request has received less than 20 community upvotes and we closed it. Still a big Thank You to you for taking the time to create this issue! To learn more about how we handle feature requests, please see our documentation.

Happy Coding!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Contribution Points | Visual Studio Code Extension API
Contribution Points. Contribution Points are a set of JSON declarations that you make in the contributes field of the package.json Extension Manifest.
Read more >
Extension Manifest File - package.json - vscode-docs
To combine extension contributions, simply edit an existing extension manifest package.json and add the new contributions and associated files. Below is an ...
Read more >
package.json - npm Docs
json file, and you can specify the engine using the "engines" field. (See below.) The name will probably be passed as an argument...
Read more >
Writing VS Code extensions in JavaScript - LogRocket Blog
The extension manifest ( package.json ) ... Like a typical Node project, we use NPM to handle the configuration and the dependencies of...
Read more >
Extension Manifest Reference - Azure DevOps | Microsoft Learn
Check out our newest documentation on extension development using the ... match the ID of the contributions defined in the manifest. JSON
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found