Skip to content

Fix source map decoding to handle case sensitivity and --out option #28273

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

Merged
merged 2 commits into from
Nov 1, 2018
Merged
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
5 changes: 3 additions & 2 deletions src/compiler/sourcemapDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace ts.sourcemaps {
fileExists(path: string): boolean;
getCanonicalFileName(path: string): string;
log(text: string): void;
useCaseSensitiveFileNames: boolean;
}

export function decode(host: SourceMapDecodeHost, mapPath: string, map: SourceMapData, program?: Program, fallbackCache = createSourceFileLikeCache(host)): SourceMapper {
Expand All @@ -79,7 +80,7 @@ namespace ts.sourcemaps {
// if no exact match, closest is 2's compliment of result
targetIndex = ~targetIndex;
}
if (!maps[targetIndex] || comparePaths(loc.fileName, maps[targetIndex].sourcePath, sourceRoot) !== 0) {
if (!maps[targetIndex] || comparePaths(loc.fileName, maps[targetIndex].sourcePath, sourceRoot, !host.useCaseSensitiveFileNames) !== 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should that ignoreCase parameter in comparePaths be mandatory?

It doesn't look like is EVER safe to assume 'ignore case' without consulting the actual host parameter.

return loc;
}
return { fileName: toPath(map.file!, sourceRoot, host.getCanonicalFileName), position: maps[targetIndex].emittedPosition }; // Closest pos
Expand Down Expand Up @@ -129,7 +130,7 @@ namespace ts.sourcemaps {
}

function compareProcessedPositionSourcePositions(a: ProcessedSourceMapPosition, b: ProcessedSourceMapPosition) {
return comparePaths(a.sourcePath, b.sourcePath, sourceRoot) ||
return comparePaths(a.sourcePath, b.sourcePath, sourceRoot, !host.useCaseSensitiveFileNames) ||
compareValues(a.sourcePosition, b.sourcePosition);
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ namespace ts {
const useCaseSensitiveFileNames = hostUsesCaseSensitiveFileNames(host);
const getCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames);

const sourceMapper = getSourceMapper(getCanonicalFileName, currentDirectory, log, host, () => program);
const sourceMapper = getSourceMapper(useCaseSensitiveFileNames, currentDirectory, log, host, () => program);

function getValidSourceFile(fileName: string): SourceFile {
const sourceFile = program.getSourceFile(fileName);
Expand Down
10 changes: 8 additions & 2 deletions src/services/sourcemaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ namespace ts {
}

export function getSourceMapper(
getCanonicalFileName: GetCanonicalFileName,
useCaseSensitiveFileNames: boolean,
currentDirectory: string,
log: (message: string) => void,
host: LanguageServiceHost,
getProgram: () => Program,
): SourceMapper {
const getCanonicalFileName = createGetCanonicalFileName(useCaseSensitiveFileNames);
let sourcemappedFileCache: SourceFileLikeCache;
return { tryGetOriginalLocation, tryGetGeneratedLocation, toLineColumnOffset, clearCache };

Expand Down Expand Up @@ -56,6 +57,7 @@ namespace ts {
return file.sourceMapper = sourcemaps.decode({
readFile: s => host.readFile!(s), // TODO: GH#18217
fileExists: s => host.fileExists!(s), // TODO: GH#18217
useCaseSensitiveFileNames,
getCanonicalFileName,
log,
}, mapFileName, maps, getProgram(), sourcemappedFileCache);
Expand Down Expand Up @@ -105,7 +107,11 @@ namespace ts {

function tryGetGeneratedLocation(info: sourcemaps.SourceMappableLocation): sourcemaps.SourceMappableLocation | undefined {
const program = getProgram();
const declarationPath = getDeclarationEmitOutputFilePathWorker(info.fileName, program.getCompilerOptions(), currentDirectory, program.getCommonSourceDirectory(), getCanonicalFileName);
const options = program.getCompilerOptions();
const outPath = options.outFile || options.out;
const declarationPath = outPath ?
removeFileExtension(outPath) + Extension.Dts :
getDeclarationEmitOutputFilePathWorker(info.fileName, program.getCompilerOptions(), currentDirectory, program.getCommonSourceDirectory(), getCanonicalFileName);
if (declarationPath === undefined) return undefined;
const declarationFile = getFile(declarationPath);
if (!declarationFile) return undefined;
Expand Down
46 changes: 39 additions & 7 deletions src/testRunner/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10481,20 +10481,28 @@ declare class TestLib {
TestFSWithWatch.getTsBuildProjectFile(project, "index.ts"),
];
}
it("does not error on container only project", () => {
const project = "container";
const containerLib = getProjectFiles("container/lib");
const containerExec = getProjectFiles("container/exec");
const containerCompositeExec = getProjectFiles("container/compositeExec");
const containerConfig = TestFSWithWatch.getTsBuildProjectFile(project, "tsconfig.json");
const files = [libFile, ...containerLib, ...containerExec, ...containerCompositeExec, containerConfig];

const project = "container";
const containerLib = getProjectFiles("container/lib");
const containerExec = getProjectFiles("container/exec");
const containerCompositeExec = getProjectFiles("container/compositeExec");
const containerConfig = TestFSWithWatch.getTsBuildProjectFile(project, "tsconfig.json");
const files = [libFile, ...containerLib, ...containerExec, ...containerCompositeExec, containerConfig];

function createHost() {
const host = createServerHost(files);

// ts build should succeed
const solutionBuilder = tscWatch.createSolutionBuilder(host, [containerConfig.path], {});
solutionBuilder.buildAllProjects();
assert.equal(host.getOutput().length, 0);

return host;
}

it("does not error on container only project", () => {
const host = createHost();

// Open external project for the folder
const session = createSession(host);
const service = session.getProjectService();
Expand All @@ -10521,6 +10529,30 @@ declare class TestLib {
assert.deepEqual(semanticDiagnostics, []);
});
});

it("can successfully find references with --out options", () => {
const host = createHost();
const session = createSession(host);
openFilesForSession([containerCompositeExec[1]], session);
const service = session.getProjectService();
checkNumberOfProjects(service, { configuredProjects: 1 });
const locationOfMyConst = protocolLocationFromSubstring(containerCompositeExec[1].content, "myConst");
const response = session.executeCommandSeq<protocol.RenameRequest>({
command: protocol.CommandTypes.Rename,
arguments: {
file: containerCompositeExec[1].path,
...locationOfMyConst
}
}).response as protocol.RenameResponseBody;


const myConstLen = "myConst".length;
const locationOfMyConstInLib = protocolLocationFromSubstring(containerLib[1].content, "myConst");
assert.deepEqual(response.locs, [
{ file: containerCompositeExec[1].path, locs: [{ start: locationOfMyConst, end: { line: locationOfMyConst.line, offset: locationOfMyConst.offset + myConstLen } }] },
{ file: containerLib[1].path, locs: [{ start: locationOfMyConstInLib, end: { line: locationOfMyConstInLib.line, offset: locationOfMyConstInLib.offset + myConstLen } }] }
]);
});
});

describe("tsserverProjectSystem duplicate packages", () => {
Expand Down
3 changes: 2 additions & 1 deletion tests/projects/container/compositeExec/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"outFile": "../built/local/compositeExec.js",
"composite": true
"composite": true,
"declarationMap": true
},
"files": [
"index.ts"
Expand Down