Skip to content

Commit

Permalink
fix: workaround for pnpm and TMPDIR (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
dargmuesli authored Mar 6, 2023
1 parent 838bf9a commit 1f43c26
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
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 @@ -106,7 +107,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");
});
});
});

0 comments on commit 1f43c26

Please sign in to comment.