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

perf(pnp): reduce work done to find zip files and the pnp hook #1474

Merged
merged 17 commits into from
Jun 19, 2020
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
101 changes: 58 additions & 43 deletions .pnp.js

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

34 changes: 34 additions & 0 deletions .yarn/versions/d11469e9.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
releases:
"@yarnpkg/cli": prerelease
"@yarnpkg/fslib": prerelease
"@yarnpkg/pnp": prerelease
"@yarnpkg/pnpify": prerelease

declined:
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-node-modules"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/json-proxy"
- "@yarnpkg/shell"
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const mte = generatePkgDriver({
[`YARN_ENABLE_INLINE_BUILDS`]: `false`,
// Otherwise we would more often test the fallback rather than the real logic
[`YARN_PNP_FALLBACK_MODE`]: `none`,
// Otherwise tests fail on systems where this is globally set to true
[`YARN_ENABLE_GLOBAL_CACHE`]: `false`,
...rcEnv,
...env,
},
Expand Down
55 changes: 21 additions & 34 deletions packages/yarnpkg-fslib/sources/ZipOpenFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {Filename, FSPath, PortablePath}

const ZIP_FD = 0x80000000;

const FILE_PARTS_REGEX = /.*?(?<!\/)\.zip(?=\/|$)/;

export type ZipOpenFSOptions = {
baseFs?: FakeFS<PortablePath>,
filter?: RegExp | null,
Expand Down Expand Up @@ -59,9 +61,6 @@ export class ZipOpenFS extends BasePortableFakeFS {
this.filter = filter;
this.maxOpenFiles = maxOpenFiles;
this.readOnlyArchives = readOnlyArchives;

this.isZip = new Set();
this.notZip = new Set();
}

getExtractHint(hints: ExtractHintOptions) {
Expand Down Expand Up @@ -690,48 +689,36 @@ export class ZipOpenFS extends BasePortableFakeFS {
if (this.filter && !this.filter.test(p))
return null;

const parts = p.split(/\//g);

for (let t = 2; t <= parts.length; ++t) {
const archivePath = parts.slice(0, t).join(`/`) as PortablePath;
let filePath = `` as PortablePath;

if (this.notZip.has(archivePath))
continue;
while (true) {
const parts = FILE_PARTS_REGEX.exec(p.substr(filePath.length));
if (!parts)
return null;

if (this.isZip.has(archivePath))
return {archivePath, subPath: this.pathUtils.resolve(PortablePath.root, parts.slice(t).join(`/`) as PortablePath)};
filePath = this.pathUtils.join(filePath, parts[0] as PortablePath);

let realArchivePath = archivePath;
let stat;
if (this.isZip.has(filePath) === false) {
if (this.notZip.has(filePath))
continue;

while (true) {
try {
stat = this.baseFs.lstatSync(realArchivePath);
} catch (error) {
if (!this.baseFs.lstatSync(filePath).isFile()) {
this.notZip.add(filePath);
continue;
}
} catch {
return null;
}

if (stat.isSymbolicLink()) {
realArchivePath = this.pathUtils.resolve(this.pathUtils.dirname(realArchivePath), this.baseFs.readlinkSync(realArchivePath));
} else {
break;
}
this.isZip.add(filePath);
}

const isZip = stat.isFile() && this.pathUtils.extname(realArchivePath) === `.zip`;

if (isZip) {
this.isZip.add(archivePath);
return {archivePath, subPath: this.pathUtils.resolve(PortablePath.root, parts.slice(t).join(`/`) as PortablePath)};
} else {
this.notZip.add(archivePath);
if (stat.isFile()) {
return null;
}
}
return {
archivePath: filePath,
subPath: this.pathUtils.resolve(PortablePath.root, p.substr(filePath.length) as PortablePath),
};
}

return null;
}

private limitOpenFiles(max: number) {
Expand Down
34 changes: 34 additions & 0 deletions packages/yarnpkg-fslib/tests/ZipOpenFS.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {getLibzipSync} from '@yarnpkg/libzip';

import {ppath, npath, Filename} from '../sources/path';
import {ZipOpenFS} from '../sources';

describe(`ZipOpenFS`, () => {
it(`can read from a zip file`, () => {
const fs = new ZipOpenFS({libzip: getLibzipSync()});

const content = fs.readFileSync(
ppath.join(
npath.toPortablePath(__dirname),
`fixtures/foo.zip/foo.txt` as Filename
),
`utf8`
);

expect(content).toEqual(`foo\n`);
});

it(`can read from a zip file in a path containing .zip`, () => {
const fs = new ZipOpenFS({libzip: getLibzipSync()});

const content = fs.readFileSync(
ppath.join(
npath.toPortablePath(__dirname),
`fixtures/folder.zip/foo.zip/foo.txt` as Filename
),
`utf8`
);

expect(content).toEqual(`foo\n`);
});
});
Binary file not shown.
Binary file added packages/yarnpkg-fslib/tests/fixtures/foo.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions packages/yarnpkg-pnp/sources/loader/applyPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export function applyPatch(pnpapi: PnpApi, opts: ApplyPatchOptions) {

const originalModuleResolveFilename = Module._resolveFilename;

Module._resolveFilename = function(request: string, parent: NodeModule | null | undefined, isMain: boolean, options?: {[key: string]: any}) {
Module._resolveFilename = function(request: string, parent: (NodeModule & {pnpApiPath?: PortablePath}) | null | undefined, isMain: boolean, options?: {[key: string]: any}) {
if (builtinModules.has(request))
return request;

Expand Down Expand Up @@ -260,7 +260,9 @@ export function applyPatch(pnpapi: PnpApi, opts: ApplyPatchOptions) {
: null;

if (absoluteRequest !== null) {
const apiPath = opts.manager.findApiPathFor(absoluteRequest);
const apiPath = parentDirectory === npath.dirname(absoluteRequest) && parent?.pnpApiPath
? parent.pnpApiPath
: opts.manager.findApiPathFor(absoluteRequest);

if (apiPath !== null) {
issuerSpecs.unshift({
Expand Down
Loading