Skip to content

Gracefully handle incomplete package.json files #239

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 3 commits into from
Mar 16, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
.eslintcache
yarn-error.log
snapshots/output/**/*.scip
tsconfig.tsbuildinfo
13 changes: 13 additions & 0 deletions snapshots/input/invalid-package-json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "invalid-package-json",
"version": "1.0.0",
"description": "Example TS/JS project",
"main": "src/main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"private": true,
"packageManager": "[email protected]"
}
10 changes: 10 additions & 0 deletions snapshots/input/invalid-package-json/packages/a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@example/a",
"description": "Example TS/JS project",
"main": "src/a.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
3 changes: 3 additions & 0 deletions snapshots/input/invalid-package-json/packages/a/src/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function a(): string {
return ''
}
9 changes: 9 additions & 0 deletions snapshots/input/invalid-package-json/packages/a/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"baseUrl": ".",
"outDir": "dist"
},
"include": ["src/*"]
}
12 changes: 12 additions & 0 deletions snapshots/input/invalid-package-json/packages/b/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"description": "Example TS/JS project",
"main": "src/b.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@example/a": "1.0.0"
}
}
5 changes: 5 additions & 0 deletions snapshots/input/invalid-package-json/packages/b/src/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { a } from '@example/a'

export function b() {
return a()
}
11 changes: 11 additions & 0 deletions snapshots/input/invalid-package-json/packages/b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"baseUrl": "./src",
"sourceRoot": "src",
"outDir": "dist"
},
"include": ["src/*"],
"references": [{ "path": "../a" }]
}
15 changes: 15 additions & 0 deletions snapshots/input/invalid-package-json/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions snapshots/input/invalid-package-json/pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages:
- 'packages/*'
24 changes: 24 additions & 0 deletions snapshots/input/invalid-package-json/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"extends": "@sourcegraph/tsconfig",
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"allowJs": false,
"moduleResolution": "node",
"esModuleInterop": true,
"lib": ["esnext", "dom", "dom.iterable"],
"sourceMap": true,
"declaration": true,
"declarationMap": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"noErrorTruncation": true,
"importHelpers": true,
"resolveJsonModule": true,
"composite": true,
"outDir": "out",
"rootDir": "."
},
"include": [],
"exclude": ["out", "node_modules", "dist"]
}
8 changes: 8 additions & 0 deletions snapshots/output/invalid-package-json/packages/a/src/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function a(): string {
// definition @example/a HEAD src/`a.ts`/
//documentation ```ts\nmodule "a.ts"\n```
// ^ definition @example/a HEAD src/`a.ts`/a().
// documentation ```ts\nfunction a(): string\n```
return ''
}

4 changes: 2 additions & 2 deletions src/FileIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ export class FileIndexer {
}
if (ts.isSourceFile(node)) {
const package_ = this.packages.symbol(node.fileName)
if (!package_) {
return this.cached(node, ScipSymbol.empty())
if (package_.isEmpty()) {
return this.cached(node, ScipSymbol.anonymousPackage())
}
return this.cached(node, package_)
}
Expand Down
27 changes: 17 additions & 10 deletions src/Packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,35 @@ export class Packages {
if (typeof name === 'string' && typeof version === 'string') {
return this.cached(filePath, ScipSymbol.package(name, version))
}
if (typeof name === 'string') {
// The version field is missing so we fallback to `"HEAD"`
return this.cached(filePath, ScipSymbol.package(name, 'HEAD'))
}
// Fallback to an anonymous package because we found a package.json but
// were unable to parse the name and version.
return this.cached(filePath, ScipSymbol.anonymousPackage())
}
} catch (error) {
console.error(`error parsing ${packageJsonPath}`, error)
return this.cached(filePath, ScipSymbol.empty())
return this.cached(filePath, ScipSymbol.anonymousPackage())
}

if (filePath === this.projectRoot) {
return this.cached(filePath, ScipSymbol.empty())
// Don't look for package.json in a parent directory of the root.
return this.cached(filePath, ScipSymbol.anonymousPackage())
}

const dirname = path.dirname(filePath)
if (dirname === filePath) {
return this.cached(filePath, ScipSymbol.empty())
// Avoid infinite recursion when `path.dirname(path) === path`
return this.cached(filePath, ScipSymbol.anonymousPackage())
}

const owner = this.symbol(dirname)
if (owner) {
return this.cached(
filePath,
ScipSymbol.global(owner, packageDescriptor(path.basename(filePath)))
)
}
return this.cached(filePath, ScipSymbol.empty())
return this.cached(
filePath,
ScipSymbol.global(owner, packageDescriptor(path.basename(filePath)))
)
}

private cached(filePath: string, sym: ScipSymbol): ScipSymbol {
Expand Down
4 changes: 4 additions & 0 deletions src/ScipSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class ScipSymbol {
return new ScipSymbol(`scip-typescript npm ${name} ${version} `)
}

public static anonymousPackage(): ScipSymbol {
return ScipSymbol.package('.', '.')
}

public static global(
owner: ScipSymbol,
descriptor: scip.scip.Descriptor
Expand Down