When you have multiple package directories, you actually associate just one of those with a given package.
Something like this:
{
"namespace": "namespace",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "48.0",
"packageDirectories": [
{
"path": "force-app",
"default": true,
"package": "My Package",
"versionNumber": "1.3.0.NEXT",
"versionDescription": "Yes, this is my 2GP",
"ancestorId": "My [email protected]",
"definitionFile": "config/project-scratch-def.json",
"postInstallScript": "namespace.MyInstaller" ]
},
{
"path": "force-unpackaged"
}
],
"packageAliases": {
"My Package": "0HoXX000000xxxxXXX",
"My [email protected]": "04t2Z000000yyyyXXX"
}
}
Defines:
- The package's package directory (force-app).
- An "unpackaged" package directory that gets deployed but that is not incorporated into the package itself (force-unpackaged).
(NB: This is a sanitised version of a real sfdx-project.json
where the material in force-unpackaged
is most definitely not included in our 2GP.)
Importantly a package is created from a single package directory. This is how Salesforce can apply the "monorepo" approach; multiple packages defined in a single git repository by simply being sibling package directories in the one sfdx-project.json. (I do not follow the "monorepo" approach, but this simplifies handling of inter-package dependencies, when the packages are all 2GPs and share the same namespace.)
The key values are those "package", "versionNumber" and (as needed) "ancestorId" in the package directory definition; these map the package directory to a given package and allows versions to be created from that directory's content.
The documentation is a bit vague but does say that:
You can group similar code and source for an application or customization to better organize your team’s repository. Later, if you decide to use second-generation packages (2GP), these directories correspond to the actual 2GP packages.
My emphasis. A directory for a 2GP; directories for multiple 2GPs.
Additionally, you'll find information about the sfdx-project.json 2GP properties in the documentation.
Best Answer
Thanks to the hint from Mohith Shrivastava I managed to get rid of this error message by using scratch org definition file with Communites and Sites and Networks enabled: