Skip to content

Commit

Permalink
Global cadl not required for cadl extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin committed Oct 20, 2022
1 parent 2e8b5fa commit 7a1995c
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 7 deletions.
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"
}
10 changes: 10 additions & 0 deletions common/changes/cadl-vs/global-cadl-not-req_2022-10-20-15-06.json
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"
}
3 changes: 2 additions & 1 deletion packages/cadl-vs/src/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ 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.",
" - 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))
Expand Down
15 changes: 15 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,16 @@ private static string GetDevelopmentCadlServerPath()
return ("node.exe", $"{serverPath} {args}", env);
}

private string? ResolveLocalCompiler(string baseDir)
{
var potentialLocation = Path.Combine(baseDir, "node_modules/@cadl-lang/compiler");
if (Directory.Exists(potentialLocation))
{
return potentialLocation;
}
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";
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
2 changes: 1 addition & 1 deletion packages/samples/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"cadl.cadl-server.path": "${workspaceFolder}/node_modules/@cadl-lang/compiler"
// "cadl.cadl-server.path": "${workspaceFolder}/node_modules/@cadl-lang/compiler"
}
5 changes: 5 additions & 0 deletions packages/samples/use-versioned-lib/main.cadl
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ namespace VersionedApi;
using Cadl.Http;

op read(): Library.PetToy;


model Broken {
type: str
}

0 comments on commit 7a1995c

Please sign in to comment.