Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions extensions/css-language-features/server/src/test/links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,24 @@ suite('Links', () => {
[{ offset: 29, value: '"~foo/hello.html"', target: getTestResource('node_modules/foo/hello.html') }], testUri, folders
);
});

test('bare module specifier resolving without tilde', async function () {

const testUri = getTestResource('about.css');
const folders = [{ name: 'x', uri: getTestResource('') }];

await assertLinks('@import "foo/hello.html|"',
[{ offset: 9, value: '"foo/hello.html"', target: getTestResource('node_modules/foo/hello.html') }], testUri, folders
);
});

test('bare module specifier resolving from subfolder', async function () {

const testUri = getTestResource('subdir/about.css');
const folders = [{ name: 'x', uri: getTestResource('') }];

await assertLinks('@import "foo/hello.html|"',
[{ offset: 9, value: '"foo/hello.html"', target: getTestResource('node_modules/foo/hello.html') }], testUri, folders
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { endsWith, startsWith } from '../utils/strings';
import { WorkspaceFolder } from 'vscode-languageserver';
import { Utils, URI } from 'vscode-uri';

function isBareModuleSpecifier(ref: string): boolean {
// A bare module specifier doesn't start with '.', '..', '/', '~', or a protocol
return !/^(\.\.?\/|\/|~|[a-z][a-z0-9+\-.]*:)/i.test(ref);
}

export function getDocumentContext(documentUri: string, workspaceFolders: WorkspaceFolder[]): DocumentContext {
function getRootFolder(): string | undefined {
for (const folder of workspaceFolders) {
Expand All @@ -30,6 +35,15 @@ export function getDocumentContext(documentUri: string, workspaceFolders: Worksp
return folderUri + ref.substring(1);
}
}
// For bare module specifiers (e.g., "some-module/style.css"),
// resolve against node_modules in the workspace root as a
// fallback, similar to how bundlers like Vite resolve imports.
if (isBareModuleSpecifier(ref)) {
const folderUri = getRootFolder();
if (folderUri) {
return Utils.resolvePath(URI.parse(folderUri), 'node_modules', ref).toString(true);
}
}
const baseUri = URI.parse(base);
const baseUriDir = baseUri.path.endsWith('/') ? baseUri : Utils.dirname(baseUri);
return Utils.resolvePath(baseUriDir, ref).toString(true);
Expand Down