Skip to content

Fix detecting default project when file is part for more than one project but not part of default configured project (eg because its output of that projet) #38429

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 1 commit into from
May 12, 2020
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
4 changes: 3 additions & 1 deletion src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,9 @@ namespace ts.server {

return project?.isSolution() ?
project.getDefaultChildProjectFromSolution(info) :
project;
project && projectContainsInfoDirectly(project, info) ?
Copy link
Member

Choose a reason for hiding this comment

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

Do the other callers of getConfigFileNameForFile and findConfiguredProjectByProjectName need this check too?

Copy link
Member Author

Choose a reason for hiding this comment

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

No.. this is the change to determine default project for the file...

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I follow. Why is direct containment more important for the default project than for other projects?

Copy link
Member Author

Choose a reason for hiding this comment

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

findConfiguredProjectByProjectName project finds the configured project by the given name and has nothing to do with scriptinfo.
Similary getConfigFileNameForFile finds config file name by traversing directories for tsconfig/jsconfig json files. Nothing else matters.

This function is what determines the default project for given script info and hence the additional logic

project :
undefined;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/testRunner/unittests/tsbuild/watchMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ namespace ts.tscWatch {
return ts.createSolutionBuilder(host, rootNames, defaultOptions || {});
}

export function ensureErrorFreeBuild(host: WatchedSystem, rootNames: readonly string[]) {
// ts build should succeed
const solutionBuilder = createSolutionBuilder(host, rootNames, {});
solutionBuilder.build();
assert.equal(host.getOutput().length, 0, JSON.stringify(host.getOutput(), /*replacer*/ undefined, " "));
}

type OutputFileStamp = [string, Date | undefined, boolean];
function transformOutputToOutputFileStamp(f: string, host: TsBuildWatchSystem): OutputFileStamp {
return [f, host.getModifiedTime(f), host.writtenFiles.has(host.toFullPath(f))] as OutputFileStamp;
Expand Down
58 changes: 58 additions & 0 deletions src/testRunner/unittests/tsserver/configuredProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,64 @@ declare var console: {
});
});
});

it("when default configured project does not contain the file", () => {
const barConfig: File = {
path: `${tscWatch.projectRoot}/bar/tsconfig.json`,
content: "{}"
};
const barIndex: File = {
path: `${tscWatch.projectRoot}/bar/index.ts`,
content: `import {foo} from "../foo/lib";
foo();`
};
const fooBarConfig: File = {
path: `${tscWatch.projectRoot}/foobar/tsconfig.json`,
content: barConfig.path
};
const fooBarIndex: File = {
path: `${tscWatch.projectRoot}/foobar/index.ts`,
content: barIndex.content
};
const fooConfig: File = {
path: `${tscWatch.projectRoot}/foo/tsconfig.json`,
content: JSON.stringify({
include: ["index.ts"],
compilerOptions: {
declaration: true,
outDir: "lib"
}
})
};
const fooIndex: File = {
path: `${tscWatch.projectRoot}/foo/index.ts`,
content: `export function foo() {}`
};
const host = createServerHost([barConfig, barIndex, fooBarConfig, fooBarIndex, fooConfig, fooIndex, libFile]);
tscWatch.ensureErrorFreeBuild(host, [fooConfig.path]);
const fooDts = `${tscWatch.projectRoot}/foo/lib/index.d.ts`;
assert.isTrue(host.fileExists(fooDts));
const session = createSession(host);
const service = session.getProjectService();
service.openClientFile(barIndex.path);
checkProjectActualFiles(service.configuredProjects.get(barConfig.path)!, [barIndex.path, fooDts, libFile.path, barConfig.path]);
service.openClientFile(fooBarIndex.path);
checkProjectActualFiles(service.configuredProjects.get(fooBarConfig.path)!, [fooBarIndex.path, fooDts, libFile.path, fooBarConfig.path]);
service.openClientFile(fooIndex.path);
checkProjectActualFiles(service.configuredProjects.get(fooConfig.path)!, [fooIndex.path, libFile.path, fooConfig.path]);
service.openClientFile(fooDts);
session.executeCommandSeq<protocol.GetApplicableRefactorsRequest>({
command: protocol.CommandTypes.GetApplicableRefactors,
arguments: {
file: fooDts,
startLine: 1,
startOffset: 1,
endLine: 1,
endOffset: 1
}
});
assert.equal(service.tryGetDefaultProjectForFile(server.toNormalizedPath(fooDts)), service.configuredProjects.get(barConfig.path));
});
});

describe("unittests:: tsserver:: ConfiguredProjects:: non-existing directories listed in config file input array", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,7 @@ ${appendDts}`
const host = createServerHost([libFile, tsbaseJson, buttonConfig, buttonSource, siblingConfig, siblingSource], { useCaseSensitiveFileNames: true });

// ts build should succeed
const solutionBuilder = tscWatch.createSolutionBuilder(host, [siblingConfig.path], {});
solutionBuilder.build();
assert.equal(host.getOutput().length, 0, JSON.stringify(host.getOutput(), /*replacer*/ undefined, " "));
tscWatch.ensureErrorFreeBuild(host, [siblingConfig.path]);
const sourceJs = changeExtension(siblingSource.path, ".js");
const expectedSiblingJs = host.readFile(sourceJs);

Expand Down
6 changes: 1 addition & 5 deletions src/testRunner/unittests/tsserver/projectReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ namespace ts.projectSystem {
describe("unittests:: tsserver:: with project references and tsbuild", () => {
function createHost(files: readonly TestFSWithWatch.FileOrFolderOrSymLink[], rootNames: readonly string[]) {
const host = createServerHost(files);

// ts build should succeed
const solutionBuilder = tscWatch.createSolutionBuilder(host, rootNames, {});
solutionBuilder.build();
assert.equal(host.getOutput().length, 0, JSON.stringify(host.getOutput(), /*replacer*/ undefined, " "));

tscWatch.ensureErrorFreeBuild(host, rootNames);
return host;
}

Expand Down