-
Notifications
You must be signed in to change notification settings - Fork 78
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
feature: Run local coursier if available to avoid using bootstraped one #1385
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,122 @@ | ||
import * as semver from "semver"; | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { ChildProcessPromise, spawn } from "promisify-child-process"; | ||
import { JavaConfig } from "./getJavaConfig"; | ||
import { OutputChannel } from "./interfaces/OutputChannel"; | ||
|
||
interface FetchMetalsOptions { | ||
serverVersion: string; | ||
serverProperties: string[]; | ||
javaConfig: JavaConfig; | ||
} | ||
|
||
export function fetchMetals({ | ||
serverVersion, | ||
serverProperties, | ||
javaConfig: { javaPath, javaOptions, extraEnv, coursierPath }, | ||
}: FetchMetalsOptions): ChildProcessPromise { | ||
/** | ||
* Without the additional object, the promises will be flattened and | ||
* we will not be able to stream the output. | ||
*/ | ||
interface PackedChildPromise { | ||
promise: ChildProcessPromise; | ||
} | ||
|
||
export async function fetchMetals( | ||
{ | ||
serverVersion, | ||
serverProperties, | ||
javaConfig: { javaPath, javaOptions, extraEnv, coursierPath }, | ||
}: FetchMetalsOptions, | ||
output: OutputChannel | ||
): Promise<PackedChildPromise> { | ||
const fetchProperties = serverProperties.filter( | ||
(p) => !p.startsWith("-agentlib") | ||
); | ||
|
||
const serverDependency = calcServerDependency(serverVersion); | ||
return spawn( | ||
javaPath, | ||
[ | ||
...javaOptions, | ||
...fetchProperties, | ||
"-Dfile.encoding=UTF-8", | ||
"-jar", | ||
coursierPath, | ||
"fetch", | ||
"-p", | ||
"--ttl", | ||
// Use infinite ttl to avoid redunant "Checking..." logs when using SNAPSHOT | ||
// versions. Metals SNAPSHOT releases are effectively immutable since we | ||
// never publish the same version twice. | ||
"Inf", | ||
serverDependency, | ||
"-r", | ||
"bintray:scalacenter/releases", | ||
"-r", | ||
"sonatype:public", | ||
"-r", | ||
"sonatype:snapshots", | ||
"-p", | ||
], | ||
{ | ||
env: { | ||
COURSIER_NO_TERM: "true", | ||
...extraEnv, | ||
...process.env, | ||
}, | ||
stdio: ["ignore"], // Due to Issue: #219 | ||
|
||
const coursierArgs = [ | ||
"fetch", | ||
"-p", | ||
"--ttl", | ||
// Use infinite ttl to avoid redunant "Checking..." logs when using SNAPSHOT | ||
// versions. Metals SNAPSHOT releases are effectively immutable since we | ||
// never publish the same version twice. | ||
"Inf", | ||
serverDependency, | ||
"-r", | ||
"bintray:scalacenter/releases", | ||
"-r", | ||
"sonatype:public", | ||
"-r", | ||
"sonatype:snapshots", | ||
"-p", | ||
]; | ||
|
||
const path = process.env["PATH"]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this work for Windows (while CI tests pass), maybe it's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
let possibleCoursier: string | undefined; | ||
if (path) { | ||
possibleCoursier = await validateCoursier(path); | ||
} | ||
|
||
function spawnDefault(): ChildProcessPromise { | ||
return spawn( | ||
javaPath, | ||
[ | ||
...javaOptions, | ||
...fetchProperties, | ||
"-Dfile.encoding=UTF-8", | ||
"-jar", | ||
coursierPath, | ||
].concat(coursierArgs), | ||
{ | ||
env: { | ||
COURSIER_NO_TERM: "true", | ||
...extraEnv, | ||
...process.env, | ||
}, | ||
} | ||
); | ||
} | ||
|
||
if (possibleCoursier) { | ||
const coursier: string = possibleCoursier; | ||
output.appendLine(`Using coursier located at ${coursier}`); | ||
return { | ||
promise: spawn(coursier, coursierArgs), | ||
}; | ||
} else { | ||
return { promise: spawnDefault() }; | ||
} | ||
} | ||
|
||
export async function validateCoursier( | ||
pathEnv: string | ||
): Promise<string | undefined> { | ||
const isWindows = process.platform === "win32"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [note] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep :P I checked the docs |
||
const possibleCoursier = pathEnv | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we want to use https://github.com/otiai10/lookpath to find commands from PATH, this library deals well with the platform differences (and it takes care of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm... I would add the library if we see any issue, but this seems to work correctly both in linux and windows. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also it seems that the alst release was in 2021 so I would rather tweak our logic if needed. |
||
.split(path.delimiter) | ||
.flatMap((p) => { | ||
try { | ||
if (fs.statSync(p).isDirectory()) { | ||
return fs.readdirSync(p).map((sub) => path.resolve(p, sub)); | ||
} else return [p]; | ||
} catch (e) { | ||
return []; | ||
} | ||
}) | ||
.find( | ||
(p) => | ||
(!isWindows && p.endsWith(path.sep + "cs")) || | ||
(!isWindows && p.endsWith(path.sep + "coursier")) || | ||
(isWindows && p.endsWith(path.sep + "cs.bat")) || | ||
(isWindows && p.endsWith(path.sep + "cs.exe")) | ||
); | ||
if (possibleCoursier) { | ||
const coursierVersion = await spawn(possibleCoursier, ["version"]); | ||
if (coursierVersion.code !== 0) { | ||
return undefined; | ||
} else { | ||
return possibleCoursier; | ||
} | ||
); | ||
} | ||
} | ||
|
||
export function calcServerDependency(serverVersion: string): string { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need these changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise it's not possible to start the extension inside VS Code, I can move it separate, but this is just a follow up from the PR that merged the language client