Skip to content

Handle empty package.json files #39937

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
Aug 6, 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
15 changes: 7 additions & 8 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3774,12 +3774,10 @@ namespace ts.server {
/*@internal*/
getPackageJsonsVisibleToFile(fileName: string, rootDir?: string): readonly PackageJsonInfo[] {
const packageJsonCache = this.packageJsonCache;
const watchPackageJsonFile = this.watchPackageJsonFile.bind(this);
const toPath = this.toPath.bind(this);
const rootPath = rootDir && toPath(rootDir);
const filePath = toPath(fileName);
const rootPath = rootDir && this.toPath(rootDir);
const filePath = this.toPath(fileName);
const result: PackageJsonInfo[] = [];
forEachAncestorDirectory(getDirectoryPath(filePath), function processDirectory(directory): boolean | undefined {
const processDirectory = (directory: Path): boolean | undefined => {
Copy link
Member Author

Choose a reason for hiding this comment

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

All the changes in this file are just to eliminate the .bind(this) which you noticed were any.

switch (packageJsonCache.directoryHasPackageJson(directory)) {
// Sync and check same directory again
case Ternary.Maybe:
Expand All @@ -3788,15 +3786,16 @@ namespace ts.server {
// Check package.json
case Ternary.True:
const packageJsonFileName = combinePaths(directory, "package.json");
watchPackageJsonFile(packageJsonFileName);
this.watchPackageJsonFile(packageJsonFileName as Path);
const info = packageJsonCache.getInDirectory(directory);
if (info) result.push(info);
}
if (rootPath && rootPath === toPath(directory)) {
if (rootPath && rootPath === this.toPath(directory)) {
return true;
}
});
};

forEachAncestorDirectory(getDirectoryPath(filePath), processDirectory);
return result;
}

Expand Down
8 changes: 3 additions & 5 deletions src/server/packageJsonCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ namespace ts.server {
};

function addOrUpdate(fileName: Path) {
const packageJsonInfo = createPackageJsonInfo(fileName, host.host);
if (packageJsonInfo !== undefined) {
packageJsons.set(fileName, packageJsonInfo);
directoriesWithoutPackageJson.delete(getDirectoryPath(fileName));
}
const packageJsonInfo = Debug.checkDefined(createPackageJsonInfo(fileName, host.host));
packageJsons.set(fileName, packageJsonInfo);
directoriesWithoutPackageJson.delete(getDirectoryPath(fileName));
}

function directoryHasPackageJson(directory: Path) {
Expand Down
4 changes: 1 addition & 3 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2735,9 +2735,7 @@ namespace ts {

type PackageJsonRaw = Record<typeof dependencyKeys[number], Record<string, string> | undefined>;
const dependencyKeys = ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"] as const;
const stringContent = host.readFile(fileName);
if (!stringContent) return undefined;
Copy link
Member Author

Choose a reason for hiding this comment

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

This was the problem. Non-empty package.jsons with parse errors were already handled, but empty files were not.


const stringContent = host.readFile(fileName) || "";
const content = tryParseJson(stringContent) as PackageJsonRaw | undefined;
const info: Pick<PackageJsonInfo, typeof dependencyKeys[number]> = {};
if (content) {
Expand Down
17 changes: 17 additions & 0 deletions src/testRunner/unittests/tsserver/packageJsonInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ namespace ts.projectSystem {
assert.ok(packageJsonInfo2.peerDependencies);
assert.ok(packageJsonInfo2.optionalDependencies);
});

it("handles empty package.json", () => {
const packageJsonContent = "";
const { projectService, host } = setup([tsConfig, { path: packageJson.path, content: packageJsonContent }]);
projectService.getPackageJsonsVisibleToFile("/src/whatever/blah.ts" as Path);
const packageJsonInfo = projectService.packageJsonCache.getInDirectory("/" as Path)!;
assert.isFalse(packageJsonInfo.parseable);

host.writeFile(packageJson.path, packageJson.content);
projectService.getPackageJsonsVisibleToFile("/src/whatever/blah.ts" as Path);
const packageJsonInfo2 = projectService.packageJsonCache.getInDirectory("/" as Path)!;
assert.ok(packageJsonInfo2);
assert.ok(packageJsonInfo2.dependencies);
assert.ok(packageJsonInfo2.devDependencies);
assert.ok(packageJsonInfo2.peerDependencies);
assert.ok(packageJsonInfo2.optionalDependencies);
});
});

function setup(files: readonly File[] = [tsConfig, packageJson]) {
Expand Down