Skip to content

feat(ts-sdk): detect & configure package manager - #7864

Merged
TomChv merged 20 commits into
dagger:mainfrom
TomChv:feat/detect-pkg-manager
Aug 12, 2024
Merged

TomChv merged 20 commits into
dagger:mainfrom
TomChv:feat/detect-pkg-manager

Conversation

@TomChv

@TomChv TomChv commented Jul 10, 2024 •

Copy link
Copy Markdown
Member

Following up on #7852, this PR uses the packageManager field of the package.json to use the correct package manager.
If not, it detect the used lockfile.

This PR also includes a change to generate a lockfile (yarn v1 by default) but it can also generate a lockfile for pnpm or npm with the right version.

⚠️ It seems yarn v4, and specially corepack with yarn v4 isn't working well with Dagger, I'm writing tests for everything except yarn v4 and may handle this later.

Example

{
  "packageManager": "[email protected]" // or "[email protected]" or "[email protected]"
}

Closes #7851

@TomChv
TomChv requested review from helderco and jedevc July 10, 2024 15:51
@TomChv TomChv self-assigned this Jul 10, 2024
@TomChv
TomChv force-pushed the feat/detect-pkg-manager branch from 5f74b08 to 050f319 Compare July 10, 2024 16:53
@Xiot

Xiot commented Jul 11, 2024

Copy link
Copy Markdown

⚠️ It seems yarn v4, and specially corepack with yarn v4 isn't working well with Dagger, I'm writing tests for everything except yarn v4 and may handle this later.

What issue are you running into with yarn v4?
A potential issue that you will probably run into is that since the users module has a @dagger.io/dagger in dependencies, it will install them as well when yarn install (for example) is run.

@TomChv

TomChv commented Jul 15, 2024

Copy link
Copy Markdown
Member Author

What issue are you running into with yarn v4?
A potential issue that you will probably run into is that since the users module has a @dagger.io/dagger in dependencies, it will install them as well when yarn install (for example) is run.

It doesn't resolve dependencies correctly and output that there's no type Container in the basic quickstart example.
I'm super confused about that and think it might be because of the worspace logic? Since it's not included in the package.json?

@Xiot

Xiot commented Jul 15, 2024

Copy link
Copy Markdown

It doesn't resolve dependencies correctly and output that there's no type Container in the basic quickstart example.
I'm super confused about that and think it might be because of the worspace logic? Since it's not included in the package.json?

One of the issues is due to the fact that that you aren't using the node_modules linker. To enable that you would need a .yarnrc file with

nodeLinker: node-modules

Since the users module isn't using 'pnp', it will just check in the node_modules folder.

The 'workspace logic' should only really affect the case where you try to do a yarn install inside of the sdk directory from the users module.

(By 'users module', I'm referring to the 'dagger' folder and not the 'sdk' folder)

@TomChv

TomChv commented Jul 15, 2024

Copy link
Copy Markdown
Member Author

One of the issues is due to the fact that that you aren't using the node_modules linker. To enable that you would need a .yarnrc file with

Okay, I'll take care of that in a follow up PR then, supporting yarn v4 is something different thant the package manager configuration

@TomChv

TomChv commented Jul 16, 2024

Copy link
Copy Markdown
Member Author

Update: on test failing left: TestTypescriptWithOtherModuleTypes

My guess is that by resolving the modules differently, it fails to provide the corresponding symbol of the dependencies, I'm trying to find a way to make it work.

@TomChv

TomChv commented Jul 16, 2024

Copy link
Copy Markdown
Member Author

Ok the bug should be fixed! There was an issue with the alias resolution.

@TomChv
TomChv force-pushed the feat/detect-pkg-manager branch from 7f6e9df to d244011 Compare July 16, 2024 16:32
Comment thread sdk/typescript/runtime/main.go Outdated
// determine the package manager to use. Otherwise, it will use the default
// package manager based on the lock files present in the module.
//
// If none of the above works, it will use yarn.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't match the code below, since the default case returns an error

@TomChv TomChv Jul 16, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the default case is returning yarn, we're going in the default case only if entries packageManager has a value, and if the entry is wrong there, then it will return an error because something is wrong with the user setup

@Xiot

Xiot commented Jul 16, 2024

Copy link
Copy Markdown

Okay, I'll take care of that in a follow up PR then, supporting yarn v4 is something different thant the package manager configuration

It's not really a yarn-v4 thing. When install (for any package manager) is run, it will install the dependencies for the sdk as well since its listed as a dependency. Since the 'user' module doesn't have access to the lock file, these intermediate dependencies will also change.

The fact that the sdk is using the pnp linker also means that the dependencies will never match, regardless of which package manager is used

@TomChv

TomChv commented Jul 16, 2024

Copy link
Copy Markdown
Member Author

It's not really a yarn-v4 thing. When install (for any package manager) is run, it will install the dependencies for the sdk as well since its listed as a dependency. Since the 'user' module doesn't have access to the lock file, these intermediate dependencies will also change.

The fact that the sdk is using the pnp linker also means that the dependencies will never match, regardless of which package manager is used

That's strange it's only failing with yarn v4 then? Other package manager works fine

@Xiot

Xiot commented Jul 16, 2024

Copy link
Copy Markdown

That's strange it's only failing with yarn v4 then? Other package manager works fine

So pnpm isolates its dependencies, so that may not be an issue, but I'm pretty sure that npm does. It may still work, but its not going to pick up the yarn.lock and apply them, so there is still the potential (depending on your version ranges in the sdk) that it will get run with different versions.

@TomChv

TomChv commented Jul 16, 2024

Copy link
Copy Markdown
Member Author

So pnpm isolates its dependencies, so that may not be an issue, but I'm pretty sure that npm does. It may still work, but its not going to pick up the yarn.lock and apply them, so there is still the potential (depending on your version ranges in the sdk) that it will get run with different versions.

If you set up npm, it will generate & pick up the package-lock.json file, for yarn the yarn.lock etc...

However, it seems the fix I made on this PR also fixes the issue with yarn v4, it seems the compiler API does not resolve the symbols the same way depending of the package manager, that's pretty funny.

@Xiot

Xiot commented Jul 16, 2024

Copy link
Copy Markdown

I know it will pick up the other lock file if it's at the root. Wasn't sure if it was going to pick it up from another folder.

Yarn patches typescript I believe. Although I never looked into what it actually changed

@TomChv

TomChv commented Jul 17, 2024

Copy link
Copy Markdown
Member Author

Yarn patches typescript I believe. Although I never looked into what it actually changed

Yeah, that's quite strange... but at least this PR will improve the DX :D

@helderco helderco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's enough comments for now 😅

Comment thread sdk/typescript/runtime/main.go Resolved
Comment on lines +672 to +689
With(daggerExec("init", "--name=Package-Detection", "--sdk=typescript", "--source=.")).
WithoutFile("yarn.lock").
WithoutFile("package.json").
WithNewFile("package.json", `
{
"dependencies": {
"typescript": "^5.3.2",
"@dagger.io/dagger": "./sdk"
},
"packageManager": "[email protected]"
}`).
With(daggerExec("develop"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also have the runtime not create package.json on init if it's already there. That's what's done in Python:

if !d.HasFile(toml) {
d.AddFile(toml, template.File(toml))
}

You can see in tests, the customizations are added before init:

out, err := daggerCliBase(t, c).
With(pyprojectExtra(`requires-python = ">=3.10"`)).
With(source).
With(daggerInitPython()).
With(daggerCall("relaxed")).

Same for the lock.

@TomChv TomChv Jul 18, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I check if the package json already exist here:

if packageJSONExist {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, then what I'm saying is that instead of:

  1. dagger init
  2. change package.json
  3. dagger develop
  4. dagger call

You do:

  1. Add custom package.json
  2. dagger init
  3. dagger call

Comment thread sdk/typescript/runtime/main.go Resolved
Comment thread sdk/typescript/runtime/main.go Resolved
Comment thread sdk/typescript/runtime/main.go Resolved
Comment thread sdk/typescript/runtime/main.go Resolved
Comment thread sdk/typescript/runtime/main.go Resolved
Comment thread sdk/typescript/runtime/main.go Resolved
@TomChv
TomChv force-pushed the feat/detect-pkg-manager branch from aaef0d5 to e88ea31 Compare July 18, 2024 17:09
@TomChv

TomChv commented Jul 18, 2024

Copy link
Copy Markdown
Member Author

@helderco I did some improvement in the PR to improve the code readability and steps.

I now store module related config in moduleConfig so it's easier to reuse everywhere.
For the installSDK, I would prefer doing that in another follow up PR as I explained in my comment.

@TomChv
TomChv force-pushed the feat/detect-pkg-manager branch 3 times, most recently from f84ff3d to cdee677 Compare July 19, 2024 15:58
@TomChv

TomChv commented Aug 12, 2024

Copy link
Copy Markdown
Member Author

Stepping back, here's a question: Should we recommend that dagger.json and dagger/ be above the target folder? Most of my comments/critiques/objections to this implementation have been because I didn't want to move all my folders down a level in my monorepo to support dagger. However, I wonder if it might not be a case of caveat emptor if you choose to use dagger directly inside your target folder like I did.

In my your dagger.json should be at the root of your repo but the source in a sub directory so you do not mixup your dependencies.

For example

.
.git
dagger.json // -> source ./ci
ci/
--- package.json
--- package-lock.json
--- tsconfig
--- src/index.ts
package.json // your project package.json
xx... rest of your repo

@drmikecrowe

drmikecrowe commented Aug 12, 2024 •

Copy link
Copy Markdown

In my your dagger.json should be at the root of your repo but the source in a sub directory so you do not mixup your dependencies.

OK, I've deleted much of my comment because I misread @TomChv's message.

For reference, I did this (and I've been doing ci/cd for a while):

  • Came to dagger.io and perused the docs (guilty for assuming)
  • Went to the quickstart and followed the quickstart in MY repo
  • Churned

@jpadams -- Food for thought based on our call and new user experiences

@Xiot

Xiot commented Aug 12, 2024

Copy link
Copy Markdown

My case is slightly different than @drmikecrowe , since I have all of my dagger code in a separate repository than my application code. My concerns are centered around the dependencies of the sdk and the rest of my dagger module. (I'm using the node_modules linker for yarn)

In my your dagger.json should be at the root of your repo but the source in a sub directory so you do not mixup your dependencies.

How would this fix 'mixing up your dependencies'? Since dagger is referenced as a dependency, calling yarn install will install the dependencies of dagger itself and use the lockfile of the root of the repo (unless the dagger module is not part of the workspace and has its own yarn.lock. In that case it will use the local lock file)

Maybe dagger init should throw an error if package.json is in the current folder?

I think what we need is a clear explanation of what you think the folder structure should be for both when dagger is in a monorepo and when its in its own repo.

@drmikecrowe

Copy link
Copy Markdown

In my your dagger.json should be at the root of your repo but the source in a sub directory so you do not mixup your dependencies.

I now believe this must be our requirement:

.
.git
dagger.json // -> source ./ci
ci/
--- package.json
--- package-lock.json
--- tsconfig
--- src/index.ts
project/ (or source)
--- package.json // your project package.json
--- xx... rest of your repo

In short, I think dagger's package.json and the project's package.json must be in parallel folders. I don't see how they can be parent/children with any monorepo setup

Tom Chauveau added 19 commits August 12, 2024 18:03
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
@TomChv
TomChv force-pushed the feat/detect-pkg-manager branch from fa322ad to 906fc6e Compare August 12, 2024 16:03
Signed-off-by: Tom Chauveau <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

💡 Suggesting using yarn as package manager with TS SDK

4 participants