Prefer local install of adl package when running global `adl` (#552)

Prevents conflicts between two copies of adl modules in global and local
package locations.
This commit is contained in:
Nick Guerrera 2021-05-18 14:22:37 -07:00 коммит произвёл GitHub
Родитель cc90a5b228
Коммит 8ab8f8a6c3
6 изменённых файлов: 56 добавлений и 5 удалений

Просмотреть файл

@ -24,9 +24,9 @@ function resolveADLServer(context: ExtensionContext): Executable {
const nodeOptions = process.env.ADL_SERVER_NODE_OPTIONS;
const args = ["--stdio"];
// In development mode (F5 launch from source), resolve to locally built adl-server.js.
// In development mode (F5 launch from source), resolve to locally built server.js.
if (process.env.ADL_DEVELOPMENT_MODE) {
const script = context.asAbsolutePath("../adl/cmd/adl-server.js");
const script = context.asAbsolutePath("../adl/dist/server/server.js");
// we use CLI instead of NODE_OPTIONS environment variable in this case
// because --nolazy is not supported by NODE_OPTIONS.
const options = nodeOptions?.split(" ") ?? [];

Просмотреть файл

@ -1,2 +1,3 @@
#!/usr/bin/env node
await import("../dist/server/server.js");
import { runScript } from "../dist/cmd/runner.js";
await runScript("dist/server/server.js");

Просмотреть файл

@ -1,2 +1,3 @@
#!/usr/bin/env node
await import("../dist/compiler/cli.js");
import { runScript } from "../dist/cmd/runner.js";
await runScript("dist/compiler/cli.js");

Просмотреть файл

@ -0,0 +1,43 @@
import path from "path";
import resolveModule from "resolve";
import url from "url";
/**
* Run script given by relative path from @azure-tools/adl package root.
* Prefer local install resolved from cwd over current package.
*
* Prevents loading two conflicting copies of ADL modules from global and
* local package locations.
*/
export function runScript(relativePath: string): Promise<void> {
return new Promise((resolve, reject) => {
resolveModule(
"@azure-tools/adl",
{
basedir: process.cwd(),
preserveSymlinks: false,
},
(err, resolved) => {
let packageRoot: string;
if (err) {
if ((err as any).code === "MODULE_NOT_FOUND") {
// Resolution from cwd failed: use current package.
packageRoot = path.resolve(url.fileURLToPath(import.meta.url), "../../..");
} else {
reject(err);
return;
}
} else if (!resolved) {
reject(new Error("BUG: Module resolution succeeded, but didn't return a value."));
return;
} else {
// Resolution succeeded to dist/compiler/index.js in local package.
packageRoot = path.resolve(resolved, "../../..");
}
const script = path.join(packageRoot, relativePath);
const scriptUrl = url.pathToFileURL(script).toString();
resolve(import(scriptUrl));
}
);
});
}

Просмотреть файл

@ -97,6 +97,7 @@ const args = yargs(process.argv.slice(2))
array: true,
});
})
.command("info", "Show information about current ADL compiler.")
.option("debug", {
type: "boolean",
description: "Output debug log messages.",
@ -294,6 +295,9 @@ async function main() {
let action: string | number;
switch (command) {
case "info":
console.log(`Module: ${url.fileURLToPath(import.meta.url)}`);
break;
case "compile":
options = await getCompilerOptions();
await compileInput(options);

Просмотреть файл

@ -1,3 +1,4 @@
import { fileURLToPath } from "url";
import { TextDocument } from "vscode-languageserver-textdocument";
import {
Connection,
@ -21,7 +22,8 @@ let documents: TextDocuments<TextDocument>;
main();
function main() {
log(`** ADL Language Server v${adlVersion} **`);
log(`ADL language server v${adlVersion}\n`);
log(`Module: ${fileURLToPath(import.meta.url)}`);
log(`Command Line: ${JSON.stringify(process.argv)}`);
connection = createConnection(ProposedFeatures.all);