Skip to content

fix(npm-aqua-compiler): Support aquaDir inside the project's node_nodules #427

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 4 commits into from
Jan 31, 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
174 changes: 119 additions & 55 deletions packages/core/npm-aqua-compiler/src/imports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,67 @@
* limitations under the License.
*/

import { join } from "path";
import { join, resolve } from "path";
import { fileURLToPath } from "url";

import { assert, describe, expect, it } from "vitest";

import { gatherImportsFromNpm } from "./imports.js";
import { gatherImportsFromNpm, GatherImportsResult } from "./imports.js";

const prefix = join(
fileURLToPath(new URL("./", import.meta.url)),
"..",
"test",
"transitive-deps",
"project",
);

function buildResolutionKey(str: string) {
return str
.slice(prefix.length)
.split("/node_modules/")
.filter(Boolean)
.join("/");
}

function matchTree(
expected: GatherImportsResult,
actual: GatherImportsResult,
aquaToCompileDirPath: string | undefined,
) {
if (aquaToCompileDirPath !== undefined) {
aquaToCompileDirPath = resolve(aquaToCompileDirPath);
}

expect(Object.keys(actual).length).toBe(Object.keys(expected).length);

Object.entries(actual).forEach(([key, value]) => {
const resolutionKey =
key === aquaToCompileDirPath ? key : buildResolutionKey(key);

const resolutionValues = expected[resolutionKey];

assert(resolutionValues);

expect(Object.keys(value).length).toBe(
Object.keys(resolutionValues).length,
);

for (const [dep, path] of Object.entries(value)) {
if (Array.isArray(path)) {
expect(dep).toBe("");
expect(expected[resolutionKey]).toHaveProperty(dep, path);

continue;
}

expect(expected[resolutionKey]).toHaveProperty(
dep,
buildResolutionKey(path),
);
}
});
}

describe("imports", () => {
/**
Expand All @@ -35,44 +90,56 @@ describe("imports", () => {
string,
Record<string, string[] | string>
> = {
[aquaToCompileDirPath]: {
[resolve(aquaToCompileDirPath)]: {
"": globalImports,
A: "./A",
B: "./B",
A: "A",
B: "B",
},
"./A": {
C: "./C",
D: "./D",
A: {
C: "C",
D: "D",
},
"./B": {
C: "./B/C",
D: "./B/D",
B: {
C: "B/C",
D: "B/D",
},
"./C": {
D: "./C/D",
C: {
D: "C/D",
},
"./B/C": {
D: "./B/C/D",
"B/C": {
D: "B/C/D",
},
};

const prefix = join(
fileURLToPath(new URL("./", import.meta.url)),
"..",
"test",
"transitive-deps",
"project",
);
const imports = await gatherImportsFromNpm({
npmProjectDirPath,
aquaToCompileDirPath,
globalImports,
});

const buildResolutionKey = (str: string) => {
return (
"./" +
str
.slice(prefix.length)
.split("/node_modules/")
.filter(Boolean)
.join("/")
);
matchTree(expectedResolution, imports, aquaToCompileDirPath);
});

it("should resolve transitive dependencies and return a subtree when 'aquaToCompileDirPath' inside project 'node_modules' folder", async () => {
const npmProjectDirPath = "./test/transitive-deps/project";

const aquaToCompileDirPath =
"./test/transitive-deps/project/node_modules/A";

const globalImports = ["./.fluence/aqua"];

const expectedResolution: Record<
string,
Record<string, string[] | string>
> = {
[resolve(aquaToCompileDirPath)]: {
"": globalImports,
C: "C",
D: "D",
},
C: {
D: "C/D",
},
};

const imports = await gatherImportsFromNpm({
Expand All @@ -81,35 +148,32 @@ describe("imports", () => {
globalImports,
});

expect(Object.keys(imports).length).toBe(
Object.keys(expectedResolution).length,
);

Object.entries(imports).forEach(([key, value]) => {
const resolutionKey =
key === aquaToCompileDirPath ? key : buildResolutionKey(key);

const resolutionValues = expectedResolution[resolutionKey];
matchTree(expectedResolution, imports, aquaToCompileDirPath);
});

assert(resolutionValues);
it("should resolve transitive dependencies when project is empty", async () => {
const npmProjectDirPath = "./test/transitive-deps/empty-project";

expect(Object.keys(value).length).toBe(
Object.keys(resolutionValues).length,
);
const aquaToCompileDirPath =
"./test/transitive-deps/empty-project/node_modules/A";

for (const [dep, path] of Object.entries(value)) {
if (Array.isArray(path)) {
expect(dep).toBe("");
expect(expectedResolution[resolutionKey]).toHaveProperty(dep, path);
const globalImports = ["./.fluence/aqua"];

continue;
}
const expectedResolution: Record<
string,
Record<string, string[] | string>
> = {
[resolve(aquaToCompileDirPath)]: {
"": globalImports,
},
};

expect(expectedResolution[resolutionKey]).toHaveProperty(
dep,
buildResolutionKey(path),
);
}
const imports = await gatherImportsFromNpm({
npmProjectDirPath,
aquaToCompileDirPath,
globalImports,
});

matchTree(expectedResolution, imports, aquaToCompileDirPath);
});
});
52 changes: 41 additions & 11 deletions packages/core/npm-aqua-compiler/src/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { resolve } from "path";

import Arborist from "@npmcli/arborist";
import { breadth } from "treeverse";

Expand All @@ -40,7 +42,9 @@ export async function gatherImportsFromNpm({
* Traverse dependency tree to construct map
* (real path of a package) -> (real paths of its immediate dependencies)
*/
const result: GatherImportsResult = {};
let result: Record<string, Record<string, string>> = {};
const rootDepsKey = "";
const aquaDepPath = resolve(aquaToCompileDirPath ?? npmProjectDirPath);

breadth({
tree,
Expand All @@ -64,15 +68,8 @@ export async function gatherImportsFromNpm({

// Root node should have top-level property pointed to aqua dependency folder
if (node.isRoot) {
const aquaDepPath = aquaToCompileDirPath ?? npmProjectDirPath;

result[aquaDepPath] = {
...(result[aquaDepPath] ??
(globalImports.length > 0
? {
"": globalImports,
}
: {})),
result[rootDepsKey] = {
...result[rootDepsKey],
[dep.name]: dep.realpath,
};
} else {
Expand All @@ -88,5 +85,38 @@ export async function gatherImportsFromNpm({
},
});

return result;
// In case 'aquaToCompileDirPath' points to any dependency inside current project
// Only the subtree with 'aquaToCompileDirPath' as root node is returned
if (aquaToCompileDirPath !== undefined && aquaDepPath in result) {
// Other nodes which are not included in the subtree simply dropped
const newResult: Record<string, Record<string, string>> = {};

breadth({
tree: aquaDepPath,
getChildren: (node) => {
const deps = result[node];

if (deps === undefined) {
return [];
}

const isRootNode = node === aquaDepPath;
newResult[isRootNode ? rootDepsKey : node] = deps;
return Object.values(deps);
},
});

result = newResult;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { [rootDepsKey]: _, ...rest } = result;

return {
...rest,
[aquaDepPath]: {
...result[rootDepsKey],
"": globalImports,
},
};
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "empty-project",
"version": "0.1.0",
"dependencies": {}
}