-
Notifications
You must be signed in to change notification settings - Fork 4k
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
(core): CustomResourceProvider
writes to node_modules
#17460
Comments
Unsure why issue #16552 was closed. This is non-standard behavior (leading to breaking users in unexpected ways) and should be modified by the CDK. The only workaround so far is to |
I agree this is poor behavior. |
Problem still exists in CDK 1.134.0 and 2.1.0. |
A monkey-patch that you can fix this error with: import {CustomResourceProvider, CustomResourceProviderProps} from 'aws-cdk-lib';
import {Construct} from 'constructs';
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';
import * as crypto from 'crypto';
const tmp = os.tmpdir();
const getOrCreateProvider = CustomResourceProvider.getOrCreateProvider;
CustomResourceProvider.getOrCreateProvider = (
scope: Construct,
id: string,
props: CustomResourceProviderProps,
) => {
const hash = crypto
.createHash('sha1')
.update(props.codeDirectory)
.digest('hex');
const codeDirectory = path.join(tmp, `cdk-crp-${hash}`);
if (!fs.existsSync(codeDirectory)) {
fs.mkdirSync(codeDirectory);
fs.writeFileSync(
path.join(codeDirectory, 'index.js'),
`module.exports = require(${JSON.stringify(props.codeDirectory)});`,
'utf8',
);
}
return getOrCreateProvider(scope, id, {
...props,
codeDirectory,
});
}; Works with at least |
I lied. Referencing the original version above only passes import {CustomResourceProvider, CustomResourceProviderProps} from 'aws-cdk-lib';
import {Construct} from 'constructs';
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';
import * as crypto from 'crypto';
const copyFilesSync = (srcDir: string, dstDir: string) => {
fs.readdirSync(srcDir).forEach((file) => {
const src = srcDir + '/' + file;
const dst = dstDir + '/' + file;
var stat = fs.statSync(src);
if (stat && stat.isDirectory()) {
fs.mkdirSync(dst);
copyFilesSync(src, dst);
} else {
fs.writeFileSync(dst, fs.readFileSync(src));
}
});
};
const tmp = os.tmpdir();
const getOrCreateProvider = CustomResourceProvider.getOrCreateProvider;
CustomResourceProvider.getOrCreateProvider = (
scope: Construct,
id: string,
props: CustomResourceProviderProps,
) => {
const hash = crypto
.createHash('sha1')
.update(props.codeDirectory)
.digest('hex');
const codeDirectory = path.join(tmp, `cdk-crp-${hash}`);
if (!fs.existsSync(codeDirectory)) {
fs.mkdirSync(codeDirectory);
copyFilesSync(props.codeDirectory, codeDirectory);
}
return getOrCreateProvider(scope, id, {
...props,
codeDirectory,
});
}; |
This is related to #19452 |
…20953) Instead of using the source code directory as a staging directory (which, from the point of view of the consumer, is inside the `node_modules` directory), create a temporary directory for staging. Fixes #17460. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
|
…ws#20953) Instead of using the source code directory as a staging directory (which, from the point of view of the consumer, is inside the `node_modules` directory), create a temporary directory for staging. Fixes aws#17460. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
What is the problem?
On instantiation, the
CustomResourceProvider
class writes a file into thenode_modules
directory instead of creating a safe temporary directory to stage local assets. What's more, for users of Yarnberry
with PnP,node_modules
is a read-only ZipFS filesystem, which raises an exception when the write is attempted.Reproduction Steps
What did you expect to happen?
A stack would be synthesized containing a custom resource to delete objects from the above bucket in the event the bucket is ever removed from the stack.
What actually happened?
CDK CLI Version
1.131.0 (build 7560c79)
Framework Version
1.131.0
Node.js Version
v16.3.0
OS
macOS Big Sur 11.6.1
Language
Typescript
Language Version
TypeScript (4.4.4)
Other information
Previously reported as a problem in the
aws-s3
module, closed by that module's maintainer:#16552
Most likely source of the problem:
aws-cdk/packages/@aws-cdk/core/lib/custom-resource-provider/custom-resource-provider.ts
Lines 165 to 166 in 5aa6ac0
A temporary directory should be created (the
core
module providesFileSystem.mkdtemp
) and the code should be written to that instead of assuming that the module directory can accept writes. Specifics for Yarn 2.x can be viewed at:https://yarnpkg.com/features/pnp
The text was updated successfully, but these errors were encountered: