js codegen: better error and peer-dependencies (#8131)

We add the daml-types/daml-ledger libraries as peer-dependencies instead
of dependencies. This will assure that the library is provided by the
importing library and avoid the situation when two different versions of
these libraries are imported by the generated and the user code.

This fixes #8101.

CHANGELOG_BEGIN
CHANGELOG_END
This commit is contained in:
Robin Krom 2020-12-03 18:21:11 +01:00 committed by GitHub
parent 4cfb360da2
commit eb8114728a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 6 deletions

View File

@ -841,14 +841,18 @@ writeTsConfig dir =
, "include" .= (["src/**/*.ts"] :: [T.Text])
]
packageJsonDependencies :: SdkVersion -> Scope -> [Dependency] -> Value
packageJsonDependencies sdkVersion (Scope scope) dependencies = object $
packageJsonDependencies :: Scope -> [Dependency] -> Value
packageJsonDependencies (Scope scope) dependencies = object $
[ "@mojotech/json-type-validation" .= jtvVersion
, "@daml/types" .= versionToText sdkVersion
, "@daml/ledger" .= versionToText sdkVersion
] ++
[ (scope <> "/" <> pkgName) .= ("file:../" <> pkgName) | Dependency pkgName <- dependencies ]
packageJsonPeerDependencies:: SdkVersion -> Value
packageJsonPeerDependencies sdkVersion = object
[ "@daml/types" .= versionToText sdkVersion
, "@daml/ledger" .= versionToText sdkVersion
]
writePackageJson :: FilePath -> SdkVersion -> Scope -> [Dependency] -> IO ()
writePackageJson packageDir sdkVersion scope dependencies =
let packageJson = object
@ -859,7 +863,8 @@ writePackageJson packageDir sdkVersion scope dependencies =
, "main" .= ("lib/index.js" :: T.Text)
, "types" .= ("lib/index.d.ts" :: T.Text)
, "description" .= ("Generated by `daml codegen js` from SDK " <> versionToText sdkVersion)
, "dependencies" .= packageJsonDependencies sdkVersion scope dependencies
, "dependencies" .= packageJsonDependencies scope dependencies
, "peer-dependencies" .= packageJsonPeerDependencies sdkVersion
]
in
BSL.writeFile (packageDir </> "package.json") (encodePretty packageJson)

View File

@ -72,6 +72,7 @@ jest.mock('isomorphic-ws', () => class {
const Foo: Template<Foo, string, "foo-id"> = {
sdkVersion: '0.0.0-SDKVERSION',
templateId: "foo-id",
keyDecoder: jtv.string(),
keyEncode: (s: string): unknown => s,

View File

@ -31,6 +31,10 @@ export interface Serializable<T> {
*/
export interface Template<T extends object, K = unknown, I extends string = string> extends Serializable<T> {
templateId: I;
/**
* @internal
*/
sdkVersion: '0.0.0-SDKVERSION';
/**
* @internal
*/
@ -103,7 +107,7 @@ export const registerTemplate = <T extends object>(template: Template<T>): void
export const lookupTemplate = (templateId: string): Template<object> => {
const template = registeredTemplates[templateId];
if (template === undefined) {
throw Error(`Trying to look up template ${templateId}.`);
throw Error(`Failed to look up template ${templateId}. Make sure your @daml/types version agrees with the used DAML SDK version.`);
}
return template;
}
@ -290,6 +294,7 @@ export const Date: Serializable<Date> = {
encode: (d: Date) => d,
}
/**
* Used to `brand` [[ContractId]].
*/