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

Use stack-path instead of inferring --programs #115

Merged
merged 4 commits into from
Aug 27, 2024
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
19 changes: 7 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,21 +562,16 @@ function parseStackYaml(contents) {
exports.parseStackYaml = parseStackYaml;
async function getStackDirectories(stackYaml, stack, workingDirectory) {
const cwd = workingDirectory ?? process.cwd();
const stackRoot = (await stack.read(["path", "--stack-root"])).trim();
const stackPrograms = stackYaml["local-programs-path"] ?? defaultLocalProgramsPath(stackRoot);
const output = await stack.read(["path", "--stack-root", "--programs"]);
const stackPath = yaml.load(output);
const stackWorks = packagesStackWorks(stackYaml, cwd);
return { stackRoot, stackPrograms, stackWorks };
return {
stackRoot: stackPath["stack-root"],
stackPrograms: stackPath.programs,
stackWorks,
};
}
exports.getStackDirectories = getStackDirectories;
function defaultLocalProgramsPath(stackRoot) {
if (process.platform === "win32") {
const localAppData = process.env.LOCALAPPDATA;
if (localAppData) {
return (0, path_1.join)(localAppData, "Programs", "stack");
}
}
return (0, path_1.join)(stackRoot, "programs");
}
function packagesStackWorks(stackYaml, cwd) {
const packageStackWorks = (stackYaml.packages ?? [])
.filter((p) => p !== ".")
Expand Down
34 changes: 20 additions & 14 deletions src/stack-yaml.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import { parseStackYaml, getStackDirectories } from "./stack-yaml";
import { StackCLI } from "./stack-cli";

function mockStackCLI(stackRoot: string = "/home/me/.stack"): StackCLI {
const testStackRoot = "/home/me/.stack";
const testPrograms = `${testStackRoot}/programs/x86_64-linux`;
const testStackPathYaml = `
stack-root: ${testStackRoot}
programs: ${testPrograms}
`;

function mockStackCLI(): StackCLI {
const stack = new StackCLI([]);

jest
.spyOn(stack, "read")
.mockImplementation((args: string[]): Promise<string> => {
if (args[0] !== "path") {
throw new Error("StackCLI.read() is only mocked for path");
// Stringify to avoid array-comparison pitfalls
const expected = ["path", "--stack-root", "--programs"].toString();
const given = args.toString();

if (given !== expected) {
throw new Error(
`StackCLI.read() is only mocked for ${expected}, saw ${given}`,
);
}

switch (args[1]) {
case "--stack-root":
return Promise.resolve(stackRoot);
default:
throw new Error(
"StackCLI.read(path) is only mocked for --stack-root",
);
}
return Promise.resolve(testStackPathYaml);
});

return stack;
Expand All @@ -27,12 +33,12 @@ function mockStackCLI(stackRoot: string = "/home/me/.stack"): StackCLI {
describe("getStackDirectories", () => {
test("stackRoot, stackPrograms", async () => {
const stackYaml = parseStackYaml("resolver: lts-22\n");
const stack = mockStackCLI("/home/me/.stack");
const stack = mockStackCLI();

const stackDirectories = await getStackDirectories(stackYaml, stack, "");

expect(stackDirectories.stackRoot).toEqual("/home/me/.stack");
expect(stackDirectories.stackPrograms).toEqual("/home/me/.stack/programs");
expect(stackDirectories.stackRoot).toEqual(testStackRoot);
expect(stackDirectories.stackPrograms).toEqual(testPrograms);
});

describe("stackWorks", () => {
Expand Down
40 changes: 16 additions & 24 deletions src/stack-yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,31 @@ export type StackDirectories = {
stackWorks: string[];
};

// Internal type for parsing Yaml output from `stack path`
type StackPath = {
"stack-root": string;
programs: string;
};

export async function getStackDirectories(
stackYaml: StackYaml,
stack: StackCLI,
workingDirectory?: string,
): Promise<StackDirectories> {
const cwd = workingDirectory ?? process.cwd();

// Only use --stack-root, which (as of stack v2.15) won't load the environment
// and install GHC, etc. It's the only option currently safe to make use of
// outside of caching.
const stackRoot = (await stack.read(["path", "--stack-root"])).trim();

// Avoid `stack path --programs` until
// https://github.com/commercialhaskell/stack/issues/6484 is fixed.
const stackPrograms =
stackYaml["local-programs-path"] ?? defaultLocalProgramsPath(stackRoot);

// Only use --stack-root and --programs, which (as of stack v2.15.3) won't
// load the environment and install GHC, etc. These are the only options
// currently safe to make use of outside of caching.
const output = await stack.read(["path", "--stack-root", "--programs"]);
const stackPath = yaml.load(output) as StackPath;
const stackWorks = packagesStackWorks(stackYaml, cwd);

return { stackRoot, stackPrograms, stackWorks };
}

// https://docs.haskellstack.org/en/stable/yaml_configuration/#local-programs-path
function defaultLocalProgramsPath(stackRoot: string): string {
if (process.platform === "win32") {
const localAppData = process.env.LOCALAPPDATA;

if (localAppData) {
return pathJoin(localAppData, "Programs", "stack");
}
}

return pathJoin(stackRoot, "programs");
return {
stackRoot: stackPath["stack-root"],
stackPrograms: stackPath.programs,
stackWorks,
};
}

function packagesStackWorks(stackYaml: StackYaml, cwd: string): string[] {
Expand Down
Loading