Skip to content
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

Ensure our project references include transitive references #55339

Closed
wants to merge 5 commits into from
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
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,17 @@ jobs:
echo "Unused baselines:"
git diff --exit-code --name-only
fi

check-project-references:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "*"
check-latest: true
- run: npm ci

- name: Check project references
run: node ./scripts/checkProjectReferences.mjs
84 changes: 84 additions & 0 deletions scripts/checkProjectReferences.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import assert from "assert";
import fs from "fs";
import glob from "glob";
import JSONC from "jsonc-parser";
import path from "path";
import url from "url";


const __filename = url.fileURLToPath(new URL(import.meta.url));
const __dirname = path.dirname(__filename);


// This script checks that we list all transitive references in all tsconfig.json files.
// See: https://github.com/microsoft/TypeScript/issues/30608


/**
* @param {string} p
*/
function getTsconfigPath(p) {
sandersn marked this conversation as resolved.
Show resolved Hide resolved
if (fs.statSync(p).isDirectory()) {
p += "/tsconfig.json";
}
else if (!p.endsWith(".json")) {
p += ".json";
}
return p;
}

/**
* @param {string} p
*/
function getReferences(p) {
const result = getReferencesWorker(p, new Set());
assert(result);
return result;

/**
* @param {string} p
* @param {Set<string>} seen
Copy link
Member

Choose a reason for hiding this comment

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

minor quibble: there's no reason to pass seen around instead of referring to it from inside getReferencesWorker

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I had it like that before but switched it to this. I'm not picky but I'm too not sure if this PR is still needed.

*/
function getReferencesWorker(p, seen) {
const tsconfigPath = getTsconfigPath(p);
if (seen.has(tsconfigPath)) {
return undefined;
}
seen.add(tsconfigPath);

const dir = path.dirname(tsconfigPath);
const contents = JSONC.parse(fs.readFileSync(tsconfigPath, "utf8"));
const references = new Set();
for (const r of contents.references || []) {
references.add(path.resolve(dir, r.path));
}

const transitiveReferences = new Set();
for (const r of references) {
const result = getReferencesWorker(r, seen);
if (!result) continue;

const [otherReferences, otherTransitiveReferences] = result;
for (const r of otherReferences) {
transitiveReferences.add(r);
}
for (const r of otherTransitiveReferences) {
transitiveReferences.add(r);
}
}

return [references, transitiveReferences];
}
}

const paths = glob.sync("src/**/*tsconfig*.json", { cwd: path.resolve(__dirname, "..") });
for (const p of paths) {
const [references, transitiveReferences] = getReferences(p);

for (const r of transitiveReferences) {
if (!references.has(r)) {
console.error(`${p} should reference ${path.relative(path.dirname(p), r)}`);
process.exitCode = 1;
}
}
}
2 changes: 2 additions & 0 deletions src/tsserver/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
{ "path": "../services" },
{ "path": "../jsTyping" },
{ "path": "../server" },
{ "path": "../typingsInstallerCore" },
{ "path": "../deprecatedCompat" },
],
"include": ["**/*"]
}
Loading