cap-js / cds-typer

CDS type generator for JavaScript
Apache License 2.0
29 stars 10 forks source link

[BUG] A generated index.ts files has syntax errors "Duplicate Identifier ..." #288

Open D027152 opened 4 months ago

D027152 commented 4 months ago

Is there an existing issue for this?

Nature of Your Project

TypeScript

Current Behavior

When generating files in @cds-models, one of the generated files has syntac errors. e.g.

Cannot redeclare exported variable '_PaymentAgreementOutgoingAspect'. Duplicate function implementation. Duplicate identifier 'PaymentAgreementOutgoing'. Class 'PaymentAgreementOutgoing' used before its declaration.

The file is this one: https://github.tools.sap/erp4sme/crypto-for-business/blob/switch-to-cds-typer-payment-master-data/%40cds-models/sap/erp4sme/c4b/masterData/businessPartners/index.ts

Expected Behavior

Files are generated without syntax errors

Steps To Reproduce

repo https://github.tools.sap/erp4sme/crypto-for-business branch switch-to-cds-typer-payment-master-data above-mentioned files are located in https://github.tools.sap/erp4sme/crypto-for-business/tree/switch-to-cds-typer-payment-master-data/%40cds-models)

to re-generate the files run npm run cds-typer:dev

Environment

| c4b                    | github.tools.sap/erp4sme/crypto-for-business.git                           |
| ---------------------- | -------------------------------------------------------------------------- |
| @cap-js/audit-logging  | 0.8.0                                                                      |
| @cap-js/cds-types      | 0.2.0                                                                      |
| @cap-js/change-trackin | 1.0.6                                                                      |
| @cap-js/telemetry      | 0.2.3                                                                      |
| @sap/cds               | 7.9.4                                                                      |
| @sap/cds-common-conten | 2.0.0                                                                      |
| @sap/cds-compiler      | 4.9.4                                                                      |
| @sap/cds-dk            | 7.9.6                                                                      |
| @sap/cds-dk (global)   | 7.7.2                                                                      |
| @sap/cds-fiori         | 1.2.3                                                                      |
| @sap/cds-foss          | 5.0.0                                                                      |
| @sap/cds-mtxs          | 2.0.3                                                                      |
| @sap/eslint-plugin-cds | 3.0.4                                                                      |
| Node.js                | v18.18.0                                                                   |
| home                   | /Users/D027152/Documents/git/dch/crypto-for-business/node_modules/@sap/cds |

npx cds-typer --version: 0.23.0

Repository Containing a Minimal Reproducible Example

https://github.tools.sap/erp4sme/crypto-for-business

Anything else?

This is a follow-up for bug https://github.com/cap-js/cds-typer/issues/278

hakimio commented 3 months ago

Possibly related issues: #139 #144

github-actions[bot] commented 1 month ago

This issue has not been updated in a while. If it is still relevant, please comment on it to keep it open. The issue will be closed soon if it remains inactive.

georgianjalba commented 1 month ago

Hey, just checked and if I upgrade from version 0.25.0 to any newer version ( 0.26.0 or 0.27.0 ) I have this duplicate identifier issue. The issue is easily reproducible by having a service and an entity in the service with the same name as the service. As I know changing the name would normally fix the issue we have no control over the names of either the service or the entity so we need to find an alternate way of fixing this.

Would be awesome if a workaround/config we could do would exist or better yet a planned fix :P

Thanks for the help and keep up the good work!

daogrady commented 1 month ago

Hi @georgianjalba ,

thanks for investigating the root cause of this issue. In that case you should be able to circumvent the collision by specifying a custom singular and plural name (not: changing the actual name) for the offending entity:

service X {
    @singular: 'XEntity'
    @plural: 'XEntities'
    entity X {}
}

Let me know if that helps.

Best, Daniel

D027152 commented 1 month ago

Hi @georgianjalba @daogrady

The problem I described above is not related to the situation that @georgianjalba describes, as far as I can see. However, I just recognised that with cds-typer 0.27.0 this problem does no longer occur (I created this issue based on 0.23.0).

I also face a similar issue as @georgianjalba describes: two entities in the same service, both having an association to an entity called "ActivationStatusCodes", but the first associated entity is in namespace sap.erp4sme.c4b.masterData.integrations, the second is in namespace sap.erp4sme.c4b.masterData.businessPartners. This gave a duplicate identifier error, which I was able to overcome by specifying different deviating custom singular and plural names ('IntegrationActivationStatusCodes' and 'BusinessPartnerActivationStatusCodes').

daogrady commented 1 month ago

Hi @D027152 ,

thanks for the update on your situation! Does that mean the issue is resolved?

Best, Daniel

D027152 commented 1 month ago

Hi @daogrady

I don't have an issue any longer. But I understood that the solution with the singular / plural annotations is rather a workaround than a solution. However, if this is from your point of view a final solution, then I would just expect in a documentation a note that this is what someone shall do when he/she rund into the problem of duplicate identifiers.

Best regards, Stefan

daogrady commented 1 month ago

@chgeo fyi please provide minimal repro that made the introduction of named service exports necessary.

chgeo commented 1 week ago

@daogrady Here is the demo project: https://github.com/chgeo/issue-named-service-exports See its readme for further details.