Skip to content

Commit c522727

Browse files
committed
Use openapi-validator from node_modules, bypassing autorest's package manager
1 parent 49883f8 commit c522727

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

eng/tools/lint-diff/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dependencies": {
1616
"@azure-tools/openapi-tools-common": "^1.2.2",
1717
"@azure/openapi-markdown": "^0.9.4",
18+
"@microsoft.azure/openapi-validator": "2.2.3",
1819
"autorest": "3.6.1",
1920
"commonmark": "^0.31.2"
2021
},

eng/tools/lint-diff/src/lint-diff.ts

+3-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { exec } from "node:child_process";
33
import { parseArgs, ParseArgsConfig } from "node:util";
44
import { getOpenapiType, getAllTags, getInputFiles } from "./markdown-parser.js";
55
import { sep, dirname, join } from "path";
6-
import { pathExists } from "./util.js";
6+
import { pathExists, getPathToDependency } from "./util.js";
77

88
// 64 MiB max ouptut buffers for exec
99
const MAX_EXEC_BUFFER = 64 * 1024 * 1024;
@@ -32,13 +32,6 @@ export async function main() {
3232
type: "string",
3333
short: "o",
3434
},
35-
// TODO: Bring this in through package.json. Do not use autorest's
36-
// package manager.
37-
"lint-version": {
38-
type: "string",
39-
short: "l",
40-
default: "2.2.3",
41-
},
4235
},
4336
strict: true,
4437
};
@@ -49,7 +42,6 @@ export async function main() {
4942
after: afterArg,
5043
"changed-files-path": changedFilesPath,
5144
"out-file": outFile,
52-
"lint-version": lintVersion,
5345
},
5446
} = parseArgs(config);
5547

@@ -68,11 +60,6 @@ export async function main() {
6860
console.log("--changed-files-path missing");
6961
}
7062

71-
if (!(lintVersion as string).match(/^\d+\.\d+\.\d+$/)) {
72-
validArgs = false;
73-
console.log("Invalid lint version:", lintVersion);
74-
}
75-
7663
if (!validArgs) {
7764
usage();
7865
process.exit(1);
@@ -92,7 +79,6 @@ export async function main() {
9279
afterArg as string,
9380
changedFilesPath as string,
9481
outFile as string,
95-
lintVersion as string,
9682
);
9783
}
9884

@@ -107,7 +93,6 @@ async function runLintDiff(
10793
afterPath: string,
10894
changedFilesPath: string,
10995
outFile: string,
110-
lintVersion: string,
11196
) {
11297
// TODO: Should filter happen here or upstream? (probably upstream)
11398
// TODO: NEED TO PARSE TAG CHANGES STILL
@@ -138,7 +123,7 @@ async function runLintDiff(
138123
// return;
139124
// }
140125

141-
let validator = `@microsoft.azure/openapi-validator@${lintVersion}`;
126+
const dependenciesDir = await getPathToDependency("@microsoft.azure/openapi-validator");
142127
const changedFileAndTagsMap = new Map<string, string[]>();
143128
for (const readmeAndTags of affectedTags) {
144129
const dedupedTags = await deduplicateTags(
@@ -178,7 +163,7 @@ async function runLintDiff(
178163
`--message-format=json ` +
179164
`--openapi-type=${openApiType} ` +
180165
`--openapi-subtype=${openApiSubType} ` +
181-
`--use=${validator} ` +
166+
`--use=${dependenciesDir} ` +
182167
`${tagArg} ` +
183168
`${changedFilePath}`;
184169

eng/tools/lint-diff/src/util.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { access } from "fs/promises";
1+
import { access, constants } from "node:fs/promises";
2+
import { dirname, join } from "node:path";
3+
import { fileURLToPath } from "node:url";
24

35
/**
46
* Enumerate files in a directory that match the given string ending
@@ -23,3 +25,29 @@ export async function pathExists(path: string): Promise<boolean> {
2325
return false;
2426
}
2527
}
28+
29+
// Copied from azure-sdk-tools repo
30+
// TODO: This should probably be moved to another more general location
31+
export async function getPathToDependency(dependency: string): Promise<string> {
32+
// Example: /home/user/foo/node_modules/@autorest/bar/dist/index.js
33+
const entrypoint = fileURLToPath(import.meta.resolve(dependency));
34+
35+
// Walk up directory tree to first folder containing "package.json"
36+
let currentDir = dirname(entrypoint);
37+
while (true) {
38+
const packageJsonFile = join(currentDir, "package.json");
39+
try {
40+
// Throws if file cannot be read
41+
await access(packageJsonFile, constants.R_OK);
42+
return currentDir;
43+
} catch {
44+
const parentDir = dirname(currentDir);
45+
if (parentDir !== currentDir) {
46+
currentDir = parentDir;
47+
} else {
48+
// Reached fs root but no package.json found
49+
throw new Error(`Unable to find package.json in folder tree above '${entrypoint}'`);
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)