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 chmod error setting mode of DTS file that may not yet be written #1002

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export async function build(_options: Options) {
})
worker.on('message', (data) => {
if (data === 'error') {
reject(new Error('error occured in dts build'))
reject(new Error('error occurred in dts build'))
} else if (data === 'success') {
resolve()
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { parentPort } from 'worker_threads'
import { InputOptions, OutputOptions, Plugin } from 'rollup'
import { NormalizedOptions } from './'
import ts from 'typescript'
import hashbangPlugin from 'rollup-plugin-hashbang'
import jsonPlugin from '@rollup/plugin-json'
import { handleError } from './errors'
import { defaultOutExtension, removeFiles } from './utils'
Expand Down Expand Up @@ -167,7 +166,6 @@ const getRollupConfig = async (
plugins: [
tsupCleanPlugin,
tsResolveOptions && tsResolvePlugin(tsResolveOptions),
hashbangPlugin(),
jsonPlugin(),
ignoreFiles,
dtsPlugin.default({
Expand Down
56 changes: 48 additions & 8 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ test('onSuccess: use a function from config file', async () => {
await new Promise((resolve) => {
setTimeout(() => {
console.log('world')
resolve('')
resolve('')
}, 1_000)
})
}
Expand Down Expand Up @@ -872,6 +872,36 @@ test('shebang', async () => {
}).toThrow()
})

/**
* tsup should not attempt to `chmod +x` type definition files in the output,
* even when their corresponding module starts with a shebang.
* It might be harmless for it to do it anyway, except that
* rollup-plugin-hashbang seems to suffer from a race condition where it may
* attempt to chmod type definition files before they are written to disk,
* which fails the build with an error:
* https://github.com/egoist/tsup/issues/1001
*/
test('do not chmod +x dts files', async () => {
const { outDir, logs } = await run(
getTestName(),
{
'a.ts': `#!/usr/bin/env node\bconsole.log('a')`,
},
{
entry: ['a.ts'],
flags: ['--dts'],
}
)

if (process.platform === 'win32') {
return
}

expect(() => {
fs.accessSync(path.join(outDir, 'a.d.ts'), fs.constants.X_OK)
}).toThrow()
})

test('es5 target', async () => {
const { output, outFiles } = await run(
getTestName(),
Expand Down Expand Up @@ -1037,7 +1067,7 @@ test('use rollup for treeshaking --format cjs', async () => {
}`,
'input.tsx': `
import ReactSelect from 'react-select'

export const Component = (props: {}) => {
return <ReactSelect {...props} />
};
Expand Down Expand Up @@ -1345,9 +1375,14 @@ test('should emit a declaration file per format', async () => {
format: ['esm', 'cjs'],
dts: true
}`,
});
expect(outFiles).toEqual(['input.d.mts', 'input.d.ts', 'input.js', 'input.mjs'])
});
})
expect(outFiles).toEqual([
'input.d.mts',
'input.d.ts',
'input.js',
'input.mjs',
])
})

test('should emit a declaration file per format (type: module)', async () => {
const { outFiles } = await run(getTestName(), {
Expand All @@ -1361,6 +1396,11 @@ test('should emit a declaration file per format (type: module)', async () => {
format: ['esm', 'cjs'],
dts: true
}`,
});
expect(outFiles).toEqual(['input.cjs', 'input.d.cts', 'input.d.ts', 'input.js'])
});
})
expect(outFiles).toEqual([
'input.cjs',
'input.d.cts',
'input.d.ts',
'input.js',
])
})