Skip to content
Open
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
20 changes: 16 additions & 4 deletions src/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ function hashCode(s: string) {
}, 0)
}

// Keep dumped filenames comfortably under the filesystem's 255-byte limit
// (e.g. ENAMETOOLONG on POSIX) while keeping the deterministic hash suffix so
// distinct ids that truncate to the same prefix still map to different files.
const MAX_DUMP_FILE_NAME_LENGTH = 200

export class Debugger {
dumpDir: string | undefined
initPromise: Promise<void> | undefined
Expand Down Expand Up @@ -49,10 +54,17 @@ export class Debugger {
await fs.mkdir(this.dumpDir, { recursive: true })
}

encodeId(id: string) {
return `${id.replace(/[^\w@\-]/g, '_').replace(/_+/g, '_')}-${hashCode(
id,
)}.js`
encodeId(id: string): string {
const sanitized = id.replace(/[^\w@\-]/g, '_').replace(/_+/g, '_')
const hash = hashCode(id)
const name = `${sanitized}-${hash}.js`
if (name.length <= MAX_DUMP_FILE_NAME_LENGTH) {
return name
}
// Reserve room for `-${hash}.js` and truncate the readable prefix so the
// full filename stays within the limit. `-` is ASCII, so bytes === chars.
const maxSanitizedLength = MAX_DUMP_FILE_NAME_LENGTH - name.length + sanitized.length
return `${sanitized.slice(0, maxSanitizedLength)}-${hash}.js`
}

async recordExternalize(id: string, path: string): Promise<void> {
Expand Down
61 changes: 61 additions & 0 deletions test/test/debug.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Buffer } from 'node:buffer'
import { mkdtemp } from 'node:fs/promises'
import { join } from 'pathe'
import { describe, expect, it } from 'vitest'
import { Debugger } from '../../src/debug'

const MAX_DUMP_FILE_NAME_LENGTH = 200

function veryLongId(): string {
// A long absolute path ending in a long spec filename, mirroring the
// ENAMETOOLONG scenario from vitest-dev/vitest#7744.
return `/Volumes/External/Long/Module/Path/${'a'.repeat(400)}_very_long_spec_tsx`
}

function debuggerWithDump(dir: string): Debugger {
return new Debugger('', { dumpModules: dir })
}

describe('debugger encodeId', () => {
it('caps the encoded filename length for very long ids', () => {
const debugger_ = new Debugger('', {})
const name = debugger_.encodeId(veryLongId())

// Filesystem limits are byte-based; ensure we stay comfortably under 255.
expect(Buffer.byteLength(name)).toBeLessThanOrEqual(MAX_DUMP_FILE_NAME_LENGTH)
expect(Buffer.byteLength(name)).toBeLessThan(255)
expect(name.endsWith('.js')).toBe(true)
})

it('round-trips dumpFile/loadDump with a very long id', async () => {
const dir = await mkdtemp(join('/tmp/opencode', 'vite-node-dump-'))
const debugger_ = debuggerWithDump(dir)
const id = veryLongId()
const code = 'console.log("dumped")'

await expect(debugger_.dumpFile(id, { code, map: null })).resolves.toBeUndefined()

const loaded = await debugger_.loadDump(id)
expect(loaded?.code).toBe(code)
})

it('keeps unique filenames for distinct long ids sharing a long common prefix', () => {
const debugger_ = new Debugger('', {})
const commonPrefix = `/Volumes/very/long/shared/prefix/${'x'.repeat(400)}`

const nameA = debugger_.encodeId(`${commonPrefix}module_a.ts`)
const nameB = debugger_.encodeId(`${commonPrefix}module_b.ts`)

expect(nameA).not.toBe(nameB)
expect(Buffer.byteLength(nameA)).toBeLessThanOrEqual(MAX_DUMP_FILE_NAME_LENGTH)
expect(Buffer.byteLength(nameB)).toBeLessThanOrEqual(MAX_DUMP_FILE_NAME_LENGTH)
})

it('encodes short ids exactly as before', () => {
const debugger_ = new Debugger('', {})
// Hardcoded outputs produced by the pre-fix encodeId formula.
expect(debugger_.encodeId('src/utils/foo.ts')).toBe('src_utils_foo_ts-1252568592.js')
expect(debugger_.encodeId('src/foo/bar.ts')).toBe('src_foo_bar_ts-155658414.js')
expect(debugger_.encodeId('test/fixtures/basic.ts')).toBe('test_fixtures_basic_ts-518376679.js')
})
})