Skip to content

Fix open compiled command for monorepos #592

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

Merged
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
33 changes: 33 additions & 0 deletions server/src/buildSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This file has been generated from https://raw.githubusercontent.com/rescript-lang/rescript-compiler/master/docs/docson/build-schema.json
// with https://app.quicktype.io/ and stripped down to what we actually need for the extension.

export interface BuildSchema {
name: string;
namespace?: boolean | string;
"package-specs"?:
| Array<ModuleFormat | ModuleFormatObject>
| ModuleFormat
| ModuleFormatObject;
suffix?: SuffixSpec;
}

export enum ModuleFormat {
Commonjs = "commonjs",
Es6 = "es6",
Es6Global = "es6-global",
}

export interface ModuleFormatObject {
"in-source"?: boolean;
module: ModuleFormat;
suffix?: SuffixSpec;
}

export enum SuffixSpec {
BsCjs = ".bs.cjs",
BsJS = ".bs.js",
BsMjs = ".bs.mjs",
Cjs = ".cjs",
JS = ".js",
Mjs = ".mjs",
}
3 changes: 2 additions & 1 deletion server/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from "path";
import { ModuleFormat } from "./buildSchema";

export let platformDir =
process.arch == "arm64" ? process.platform + process.arch : process.platform;
Expand Down Expand Up @@ -49,7 +50,7 @@ export let cmiExt = ".cmi";
export let startBuildAction = "Start Build";

// bsconfig defaults according configuration schema (https://rescript-lang.org/docs/manual/latest/build-configuration-schema)
export let bsconfigModuleDefault = "commonjs";
export let bsconfigModuleDefault = ModuleFormat.Commonjs;
export let bsconfigSuffixDefault = ".js";

export let configurationRequestId = "rescript_configuration_request";
Expand Down
150 changes: 150 additions & 0 deletions server/src/lookup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import * as fs from "fs";
import * as path from "path";
import * as p from "vscode-languageserver-protocol";

import { BuildSchema, ModuleFormat, ModuleFormatObject } from "./buildSchema";
import * as c from "./constants";

const getCompiledFolderName = (moduleFormat: ModuleFormat): string => {
switch (moduleFormat) {
case "es6":
return "es6";
case "es6-global":
return "es6_global";
case "commonjs":
default:
return "js";
}
};

export const replaceFileExtension = (filePath: string, ext: string): string => {
let name = path.basename(filePath, path.extname(filePath));
return path.format({ dir: path.dirname(filePath), name, ext });
};

// Check if filePartialPath exists at directory and return the joined path,
// otherwise recursively check parent directories for it.
export const findFilePathFromProjectRoot = (
directory: p.DocumentUri | null, // This must be a directory and not a file!
filePartialPath: string
): null | p.DocumentUri => {
if (directory == null) {
return null;
}

let filePath: p.DocumentUri = path.join(directory, filePartialPath);
if (fs.existsSync(filePath)) {
return filePath;
}

let parentDir: p.DocumentUri = path.dirname(directory);
if (parentDir === directory) {
// reached the top
return null;
}

return findFilePathFromProjectRoot(parentDir, filePartialPath);
};

export const readBsConfig = (projDir: p.DocumentUri): BuildSchema | null => {
try {
let bsconfigFile = fs.readFileSync(
path.join(projDir, c.bsconfigPartialPath),
{ encoding: "utf-8" }
);

let result: BuildSchema = JSON.parse(bsconfigFile);
return result;
} catch (e) {
return null;
}
};

// Collect data from bsconfig to be able to find out the correct path of
// the compiled JS artifacts.
export const getSuffixAndPathFragmentFromBsconfig = (bsconfig: BuildSchema) => {
let pkgSpecs = bsconfig["package-specs"];
let pathFragment = "";
let module = c.bsconfigModuleDefault;
let moduleFormatObj: ModuleFormatObject = { module: module };
let suffix = c.bsconfigSuffixDefault;

if (pkgSpecs) {
if (
!Array.isArray(pkgSpecs) &&
typeof pkgSpecs !== "string" &&
pkgSpecs.module
) {
moduleFormatObj = pkgSpecs;
} else if (typeof pkgSpecs === "string") {
module = pkgSpecs;
} else if (Array.isArray(pkgSpecs) && pkgSpecs[0]) {
if (typeof pkgSpecs[0] === "string") {
module = pkgSpecs[0];
} else {
moduleFormatObj = pkgSpecs[0];
}
}
}

if (moduleFormatObj["module"]) {
module = moduleFormatObj["module"];
}

if (!moduleFormatObj["in-source"]) {
pathFragment = "lib/" + getCompiledFolderName(module);
}

if (moduleFormatObj.suffix) {
suffix = moduleFormatObj.suffix;
} else if (bsconfig.suffix) {
suffix = bsconfig.suffix;
}

return [suffix, pathFragment];
};

export const getFilenameFromBsconfig = (
projDir: string,
partialFilePath: string
): string | null => {
let bsconfig = readBsConfig(projDir);

if (!bsconfig) {
return null;
}

let [suffix, pathFragment] = getSuffixAndPathFragmentFromBsconfig(bsconfig);

let compiledPartialPath = replaceFileExtension(partialFilePath, suffix);

return path.join(projDir, pathFragment, compiledPartialPath);
};

// Monorepo helpers
export const getFilenameFromRootBsconfig = (
projDir: string,
partialFilePath: string
): string | null => {
let rootBsConfigPath = findFilePathFromProjectRoot(
path.join("..", projDir),
c.bsconfigPartialPath
);

if (!rootBsConfigPath) {
return null;
}

let rootBsconfig = readBsConfig(path.dirname(rootBsConfigPath));

if (!rootBsconfig) {
return null;
}

let [suffix, pathFragment] =
getSuffixAndPathFragmentFromBsconfig(rootBsconfig);

let compiledPartialPath = replaceFileExtension(partialFilePath, suffix);

return path.join(projDir, pathFragment, compiledPartialPath);
};
19 changes: 13 additions & 6 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
CodeLensParams,
SignatureHelpParams,
} from "vscode-languageserver-protocol";
import * as lookup from "./lookup";
import * as utils from "./utils";
import * as codeActions from "./codeActions";
import * as c from "./constants";
Expand Down Expand Up @@ -102,20 +103,26 @@ let send: (msg: p.Message) => void = (_) => {};

let findRescriptBinary = (projectRootPath: p.DocumentUri | null) =>
extensionConfiguration.binaryPath == null
? utils.findFilePathFromProjectRoot(projectRootPath, path.join(c.nodeModulesBinDir, c.rescriptBinName))
? lookup.findFilePathFromProjectRoot(
projectRootPath,
path.join(c.nodeModulesBinDir, c.rescriptBinName)
)
: utils.findBinary(extensionConfiguration.binaryPath, c.rescriptBinName);

let findPlatformPath = (projectRootPath: p.DocumentUri | null) => {
if (extensionConfiguration.platformPath != null) {
return extensionConfiguration.platformPath;
}

let rescriptDir = utils.findFilePathFromProjectRoot(projectRootPath, path.join("node_modules", "rescript"));
let rescriptDir = lookup.findFilePathFromProjectRoot(
projectRootPath,
path.join("node_modules", "rescript")
);
if (rescriptDir == null) {
return null;
}

let platformPath = path.join(rescriptDir, c.platformDir)
let platformPath = path.join(rescriptDir, c.platformDir);

// Workaround for darwinarm64 which has no folder yet in ReScript <= 9.1.4
if (
Expand All @@ -127,10 +134,10 @@ let findPlatformPath = (projectRootPath: p.DocumentUri | null) => {
}

return platformPath;
}
};

let findBscExeBinary = (projectRootPath: p.DocumentUri | null) =>
utils.findBinary(findPlatformPath(projectRootPath), c.bscExeName)
utils.findBinary(findPlatformPath(projectRootPath), c.bscExeName);

interface CreateInterfaceRequestParams {
uri: string;
Expand Down Expand Up @@ -941,7 +948,7 @@ function createInterface(msg: p.RequestMessage): p.Message {
let result = typeof response.result === "string" ? response.result : "";

try {
let resiPath = utils.replaceFileExtension(filePath, c.resiExt);
let resiPath = lookup.replaceFileExtension(filePath, c.resiExt);
fs.writeFileSync(resiPath, result, { encoding: "utf-8" });
let response: p.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
Expand Down
Loading