nodejs icon indicating copy to clipboard operation
nodejs copied to clipboard

Using BP_NODE_PROJECT_PATH should keep only the file specified in the path, not the root directory

Open pmbanugo opened this issue 2 years ago • 8 comments

Describe the Enhancement

Currently when I use the BP_NODE_PROJECT_PATH environment variable, all the files in my monorepo are copied into the final image. What I want is for the command to recognise the yarn.lock or package-lock in the root workspace and use that to build the project in the specified path, but the source code files in the final image should only be the files in the specified path

Possible Solution

No idea because I don't know how this buildpack is implemented

Motivation

I use paketo in my monorepo but I want the final image to be rid of unnecessary files.

pmbanugo avatar Sep 14 '22 12:09 pmbanugo

In your monorepo, are the sub-projects separate, or are there shared files? I ask because it is possible to run a build with a subset of the repository structure, eg. pack build my-app --path ./some-sub-directory.

ryanmoran avatar Sep 15 '22 14:09 ryanmoran

Yes, I do that already. I was suggesting this because the docs mentioned using BP_NODE_PROJECT_PATH for monorepo cases.

pmbanugo avatar Sep 15 '22 15:09 pmbanugo

I think that in the case where you'd use the BP_NODE_PROJECT_PATH environment variable, there is an expectation that the application shares some files with other parts of the monorepo and therefore we cannot safely remove those files from the image. We could update the docs to mention the --path flag on pack build as the preferred solution when your application sub-directories are independent. Does that help?

ryanmoran avatar Sep 15 '22 16:09 ryanmoran

I think mentioning that when application sub-directories are independent, --path is likely the better option. But for my use case, it's not what I want to have. I'd prefer a way to build a monorepo but only copy the files for the workspace/project I want to build.

I have a Typescript monorepo where workspaces/projects share a root tsconfig.json file. Let's say I have this file structure:

Screenshot 2022-09-19 at 11 17 12

The tsconfig.json for the product project is:

{
  "extends": "../../tsconfig.json",
  "include": ["src"],
  "exclude": ["scripts", "node_modules"],
  "compilerOptions": {
    "outDir": "dist"
  }
}

If I use that, then there's an error when building the project because it can't find the root tsconfig.json. So, I would like to be able to specify that env variable and build projects in monorepos. Currently, if I use BP_NODE_PROJECT_PATH I can't get it to build successfully, maybe I need to tweak something. The build command runs successfully if I compile normally, but using the buildpacks I get weird compile errors. If I build local and just run pack build it copies all the files including the locally built /dist folder and runs fines (but copying all the files isn't my desired goal)

Also, is there a buildpack that can detect TypeScript and automatically use the build script in the project, rather than me specifying BP_NODE_RUN_SCRIPTS=build? I think the buildpack should detect the build script just like it does with the start script.

pmbanugo avatar Sep 19 '22 09:09 pmbanugo

@ryanmoran any thoughts on this?

pmbanugo avatar Oct 19 '22 18:10 pmbanugo

There are a couple of requests here. Let me enumerate each and give some guidance on what can be done.

  1. the built image should exclude some extra files that belong to other apps

    For this, I'd suggest to try out the source-removal buildpack. We have discussed adding this to a few of the existing language families, but don't have enough data yet to make the best choices for users. Ideally, you could try using this buildpack and giving us some feedback about the settings you used to achieve your desired outcome.

  2. the buildpack should automatically detect and run the build script

    This feature was recently added in https://github.com/paketo-buildpacks/node-run-script/pull/154 and released in https://github.com/paketo-buildpacks/node-run-script/releases/tag/v0.5.0 and https://github.com/paketo-buildpacks/nodejs/releases/tag/v0.25.0. Please try those releases out and let us know if it satisfies your expectations.

If I've missed anything else in your request, please let me know.

ryanmoran avatar Oct 19 '22 22:10 ryanmoran

@ryanmoran yes I just noticed yesterday that the buildpacks automatically runs the build script. That's what I want, and I'm glad it works.

I didn't find that buildpack in the docs. Could you give me some pointers as to how I can use this with the pack cli and combine it with the nodejs buildpack

pmbanugo avatar Oct 20 '22 07:10 pmbanugo

Sorry, I should have included a link to the source-removal buildpack: https://github.com/paketo-buildpacks/source-removal. The README includes some more details, but to summarize, you could do something like the following:

pack build my-app \
  --buildpack paketo-buildpacks/nodejs \
  --buildpack paketo-buildpacks/source-removal \
  --env BP_INCLUDE_FILES=dist/*

This will remove any files from the /workspace directory that are not in the dist subfolder.

ryanmoran avatar Oct 20 '22 15:10 ryanmoran

I'm going to close this because it looks like the issue was resolved and its been a few years since the last comment/update. Please let us know if that was not the right thing to do.

mhdawson avatar Aug 06 '24 15:08 mhdawson