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

fix: workaround for pnpm and TMPDIR #123

Merged
merged 6 commits into from
Mar 6, 2023
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
5 changes: 3 additions & 2 deletions src/jiti.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "fs";
import { Module, builtinModules } from "module";
import { performance } from "perf_hooks";
import { tmpdir, platform } from "os";
import { platform } from "os";
import vm from "vm";
import { fileURLToPath, pathToFileURL } from "url";
import { dirname, join, basename, extname } from "pathe";
Expand All @@ -14,6 +14,7 @@ import { addHook } from "pirates";
import objectHash from "object-hash";
import { hasESMSyntax, interopDefault, resolvePathSync } from "mlly";
import {
getCacheDir,
isDir,
isWritable,
md5,
Expand Down Expand Up @@ -105,7 +106,7 @@ export default function createJITI(
}

if (opts.cache === true) {
opts.cache = join(tmpdir(), "node-jiti");
opts.cache = getCacheDir();
}
if (opts.cache) {
try {
Expand Down
22 changes: 22 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
import { lstatSync, accessSync, constants, readFileSync } from "fs";
import { createHash } from "crypto";
import { tmpdir } from "os";
import { join } from "pathe";
import type { PackageJson } from "pkg-types";

export function getCacheDir() {
let _tmpDir = tmpdir();

// Workaround for pnpm setting an incorrect `TMPDIR`.
// Set `JITI_RESPECT_TMPDIR_ENV` to a truthy value to disable this workaround.
// https://github.com/pnpm/pnpm/issues/6140
// https://github.com/unjs/jiti/issues/120
if (
process.env.TMPDIR &&
_tmpDir === process.cwd() &&
!process.env.JITI_RESPECT_TMPDIR_ENV
) {
const _env = process.env.TMPDIR;
delete process.env.TMPDIR;
_tmpDir = tmpdir();
process.env.TMPDIR = _env;
}

return join(_tmpDir, "node-jiti");
}

export function isDir(filename: string): boolean {
try {
const stat = lstatSync(filename);
Expand Down
43 changes: 43 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { afterEach, describe, beforeEach, it, expect, vi } from "vitest";

import { getCacheDir } from "../src/utils";

describe("utils", () => {
describe("getCacheDir", () => {
const cwd = "/cwd";
const notCwd = `${cwd}__NOT__`;

beforeEach(() => {
vi.spyOn(process, "cwd").mockImplementation(() => cwd);
});

afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllEnvs();
});

it("returns the system's TMPDIR when TMPDIR is not set", () => {
const originalTmpdir = process.env.TMPDIR;
delete process.env.TMPDIR;
expect(getCacheDir()).toBe("/tmp/node-jiti");
process.env.TMPDIR = originalTmpdir;
});

it("returns TMPDIR when TMPDIR is not CWD", () => {
vi.stubEnv("TMPDIR", notCwd);
expect(getCacheDir()).toBe("/cwd__NOT__/node-jiti");
});

it("returns the system's TMPDIR when TMPDIR is CWD", () => {
vi.stubEnv("TMPDIR", cwd);
expect(getCacheDir()).toBe("/tmp/node-jiti");
});

it("returns TMPDIR when TMPDIR is CWD and TMPDIR is kept", () => {
vi.stubEnv("TMPDIR", cwd);
vi.stubEnv("JITI_RESPECT_TMPDIR_ENV", "true");

expect(getCacheDir()).toBe("/cwd/node-jiti");
});
});
});