Skip to content

Open link to file from node module declared in SCSS import #432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
L1mer opened this issue Mar 31, 2025 · 0 comments
Open

Open link to file from node module declared in SCSS import #432

L1mer opened this issue Mar 31, 2025 · 0 comments
Assignees

Comments

@L1mer
Copy link

L1mer commented Mar 31, 2025

VSCode can't resolve file imports in SCSS that refer to node modules.

If the file in which the import is declared is not in the root folder containing node modules, VSCode tries to resolve this path by relative link. That is, if the file is in, say, project/test.scss, it will be opened in project/@example/theme/theming. If the file does not exist, VSCode should try to find it in the node modules.

I've looked at the source code a bit, and found the specific place where the error occurs:

vscode-css-languageservice/src/services/cssNavigation.ts - resolvePathToModule

protected async resolvePathToModule(_moduleName: string, documentFolderUri: string, rootFolderUri: string | undefined): Promise<string | undefined> {
	// resolve the module relative to the document. We can't use `require` here as the code is webpacked.

	const packPath = joinPath(documentFolderUri, 'node_modules', _moduleName, 'package.json');
	if (await this.fileExists(packPath)) {
		return dirname(packPath);
	} else if (rootFolderUri && documentFolderUri.startsWith(rootFolderUri) && (documentFolderUri.length !== rootFolderUri.length)) {
		return this.resolvePathToModule(_moduleName, dirname(documentFolderUri), rootFolderUri);
	}
	return undefined;
}

documentFolderUri.startsWith(rootFolderUri) compares the document uri and the root uri. The problem is that the client sends the encoded document uri to the language server:

[Trace - 11:01:31 AM] Sending request 'textDocument/documentLink - (369)'.
Params: {
    "textDocument": {
        "uri": "file:///c%3A/Users/.../scss/projects/test.scss"
    }
}

The root uri is not encoded, because documentFolderUri parameter was set with dirname function on documentUri:

export function dirname(uriString: string): string {
	return Utils.dirname(URI.parse(uriString)).toString(true);
}

The toString function has a true parameter, which means the uri will not be encoded. Therefore an error occurs in resolvePathToModule because undefined is returned.

I also looked at unit tests under the ‘SCSS node module resolving’ category. There the document uri is not encoded, so no error occurs. I don't know if this is a bug with the laguage service or VSCode passing an unencoded uri, so I am posting the issue here

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.98.2
  • OS Version: Windows Pro 11 23H2

Steps to Reproduce:

  1. Install some npm theming package with SCSS
  2. Create new directory
  3. Create new SCSS file in this directory
  4. Import some SCSS file from node package like that: @import "@example/theme/theming";
  5. Unable to open 'theming', vscode tries to find this module in directory where SCSS file is located
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants