[SalesForce] SFDX: Create unlocked package version – What is happening behind the scene

When we are creating unlocked package version in 2GP, we don't need to have packaging org at all to deploy code there, upload package etc. (as it was with first generation packaging).

So we just simply point 'force:package:version:create' command to local folder with artifacts in our DX project, and all the magic happens by itself behind the scene.

There are a few questions about what's actually going on when creating 2GP package version:

  1. Does SF use some internal/hidden scratch org to actually deploy package code there – to make sure that apex code and sobjects are valid?
  2. What if our package depends from AppExchange package – how does SF make sure that everything is fine with references to those external classes and sobjects?
  3. Does it execute all tests as well as in case of 1GP package upload? Do we have here same restrictions, e.g. all tests should succeed, >=75% of coverage etc.
  4. Assuming we converted our whole 1GP package to 2GP one (without any splitting, I know this is not quite good, but still) – could we expect that package version creation would be faster/same/longer?

Any help would be appreciated

Best Answer

I can answer some of these I think. At least we are now creating new unlocked package versions and promoting them. Our unlocked package version does depend on two managed packages.

1 - Does SF use some internal/hidden scratch org to actually deploy package code there - to make sure that apex code and sobjects are valid?

Yes, as on the reference docs "Creates a package version in the Dev Hub org." so this command takes what you have in your local source folder and then uses via the DevHub org spins up a transient scratch org and checks the metadata deploys there.

The build can fail here if for example your apex code references an unmanaged field that is present in the scratch org but hasn't been pulled locally. Also I believe it matters what you have in your project-scratch-def for the package, if for example your app depends on Notes or Chatter being enabled in the org then you must include that in project scratch def or the package will not build.

2 What if our package depends from AppExchange package - how does SF make sure that everything is fine with references to those external classes and sobjects?

You need to declare these dependencies in sfdx-project.json, below is a snippet of ours where we do this, referencing the two managed packages that the unlocked package depends on.

"packageDirectories": [
    {
        "path": "arcus-crm-core",
        "default": true,
        "package": "arcus-crm",
        "versionName": "ver 0.6",
        "versionNumber": "0.6.0.NEXT",
        "definitionFile": "config/project-scratch-def.json",
        "dependencies": [
            {
                "package": "04t0a000001rJyj"
            },
            {
                "package": "04t0a000001xvUU"
            }
        ]
    },

When you do this and create a new version then it will install the dependent packages into the transient scratch org. (I can tell as the time taken for the build increases substantially).

3 Does it execute all tests as well as in case of 1GP package upload? Do we have here same restrictions, e.g. all tests should succeed, >=75% of coverage etc.

Update for Winter 21 Code coverage is not enforced when creating a new version (always created as beta) but from the Winter release you wont be able to promote a beta version to release (requred for production installs) unless it meets 75% code coverage requirements. Source

sfdx force:package:version:promote --package "<alias of package version to promote>"

4 - Assuming we converted our whole 1GP package to 2GP one (without any splitting, I know this is not quite good, but still) - could we expect that package version creation would be faster/same/longer?

Im guessing but I suspect the same in terms of time for commands to run. I think overall its quicker though as the flow for developing, pulling, committing packaging etc is a lot smoother and easier to do from Vscode.

I cant vouch for how up to date it is but I found this FAQ really useful for gettingto grips with unlocked packages.

https://sfdc-db-gmail.github.io/unlocked-packages/faq-unlocked-pkgs#toc

Hope that helps!

Related Topic