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

Don't require a global install of cadl for the IDE extension to work #1197

Merged
merged 5 commits into from
Oct 20, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cadl-lang/compiler",
"comment": "Added a new export to only import the module resolver",
"type": "minor"
}
],
"packageName": "@cadl-lang/compiler"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "cadl-vs",
"comment": "Extension lookup for a local cadl compiler first instead of a global",
"type": "minor"
}
],
"packageName": "cadl-vs"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "cadl-vscode",
"comment": "Extension lookup for a local cadl compiler first instead of a global",
"type": "minor"
}
],
"packageName": "cadl-vscode"
}
5 changes: 3 additions & 2 deletions packages/cadl-vs/src/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ internal sealed class CadlServerNotFoundException : CadlUserErrorException
public CadlServerNotFoundException(string fileName, Exception? innerException = null)
: base(string.Join("\n", new string[]
{
$"Cadl server exectuable was not found: '{fileName}' is not found. Make sure either:",
$"Cadl server executable was not found: '{fileName}' is not found. Make sure either:",
" - cadl is installed locally at the root of this workspace or in a parent directory.",
" - cadl is installed globally with `npm install -g @cadl-lang/compiler'.",
" - cadl server path is configured with https://github.com/microsoft/cadl/blob/main/packages/cadl-vs/README.md#configure-cadl-visual-studio-extension."
}, innerException))
}), innerException)
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
{
}
}
Expand Down
20 changes: 20 additions & 0 deletions packages/cadl-vs/src/VSExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ private static string GetDevelopmentCadlServerPath()
}

var serverPath = _configuredCadlServerPath;
if ((serverPath == null || serverPath.Length == 0) && _workspaceFolder != null && _workspaceFolder.Length > 0)
{
serverPath = ResolveLocalCompiler(_workspaceFolder);
}

if (serverPath == null || serverPath.Length == 0)
{
return ("cadl-server.cmd", args, env);
Expand Down Expand Up @@ -247,6 +252,21 @@ private static string GetDevelopmentCadlServerPath()
return ("node.exe", $"{serverPath} {args}", env);
}

private string? ResolveLocalCompiler(string baseDir)
{
var current = baseDir;
while (current != null)
{
var potentialInstallDir = Path.Combine(current, "node_modules", "@cadl-lang", "compiler");
if (Directory.Exists(potentialInstallDir))
{
return potentialInstallDir;
}
current = Path.GetDirectoryName(current);
}
return null;
}

private async Task LoadSettingsAsync()
{
var workspace = _workspaceService.CurrentWorkspace;
Expand Down
33 changes: 30 additions & 3 deletions packages/cadl-vscode/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { stat } from "fs/promises";
import { resolveModule, ResolveModuleHost } from "@cadl-lang/compiler/module-resolver";
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
import { readFile, realpath, stat } from "fs/promises";
import { join } from "path";
import vscode, { commands, ExtensionContext, workspace } from "vscode";
import {
Expand Down Expand Up @@ -54,9 +55,12 @@ async function launchLanguageClient(context: ExtensionContext) {
await client.start();
} catch (e) {
if (typeof e === "string" && e.startsWith("Launching server using command")) {
const workspaceFolder = workspace.workspaceFolders?.[0]?.uri?.fsPath ?? "";

client?.error(
[
`Cadl server executable was not found: '${exe.command}' is not found. Make sure either:`,
` - cadl is installed locally at the root of this workspace ("${workspaceFolder}") or in a parent directory.`,
" - cadl is installed globally with `npm install -g @cadl-lang/compiler'.",
" - cadl server path is configured with https://github.com/microsoft/cadl#installing-vs-code-extension.",
].join("\n"),
Expand Down Expand Up @@ -92,18 +96,21 @@ async function resolveCadlServer(context: ExtensionContext): Promise<Executable>

// In production, first try VS Code configuration, which allows a global machine
// location that is not on PATH, or a workspace-specific installation.
let serverPath = workspace.getConfiguration().get("cadl.cadl-server.path") as string;
let serverPath: string | undefined = workspace.getConfiguration().get("cadl.cadl-server.path");
if (serverPath && typeof serverPath !== "string") {
throw new Error("VS Code configuration option 'cadl.cadl-server.path' must be a string");
}
const workspaceFolder = workspace.workspaceFolders?.[0]?.uri?.fsPath ?? "";

// Default to cadl-server on PATH, which would come from `npm install -g
// @cadl-lang/compiler` in a vanilla setup.
if (!serverPath) {
serverPath = await resolveLocalCompiler(workspaceFolder);
}
if (!serverPath) {
const executable = process.platform === "win32" ? "cadl-server.cmd" : "cadl-server";
return { command: executable, args, options };
}
const workspaceFolder = workspace.workspaceFolders?.[0]?.uri?.fsPath ?? "";
const variableResolver = new VSCodeVariableResolver({
workspaceFolder,
workspaceRoot: workspaceFolder, // workspaceRoot is deprecated but we still support it for backwards compatibility.
Expand All @@ -129,6 +136,26 @@ async function resolveCadlServer(context: ExtensionContext): Promise<Executable>
return { command: "node", args: [serverPath, ...args], options };
}

async function resolveLocalCompiler(baseDir: string): Promise<string | undefined> {
const host: ResolveModuleHost = {
realpath,
readFile: (path: string) => readFile(path, "utf-8"),
stat,
};
try {
const executable = await resolveModule(host, "@cadl-lang/compiler", {
baseDir,
});
if (executable.type === "module") {
return executable.path;
}
} catch (e) {
// Couldn't find the module
}

return undefined;
}

async function isFile(path: string) {
try {
const stats = await stat(path);
Expand Down
17 changes: 15 additions & 2 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,26 @@
"cadlMain": "lib/main.cadl",
"exports": {
".": "./dist/core/index.js",
"./testing": "./dist/testing/index.js"
"./testing": "./dist/testing/index.js",
"./module-resolver": "./dist/core/module-resolver.js"
},
"browser": {
"./dist/core/node-host.js": "./dist/core/node-host.browser.js",
"./dist/core/logger/console-sink.js": "./dist/core/logger/console-sink.browser.js"
},
"types": "dist/core/index.d.ts",
"typesVersions": {
"*": {
"*": [
"./dist/core/index.d.ts"
],
"testing": [
"./dist/testing/index.d.ts"
],
"module-resolver": [
"./dist/core/module-resolver.d.ts"
]
}
},
"engines": {
"node": ">=16.0.0"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/samples/.vscode/settings.json

This file was deleted.