When the backend canister name is backend, the did file generated by dfx deploy is called service.did

Use Rust.

However, .dfx/local/canister/index.js imports
import { idlFactory } from ‘./backend.did.js’;
but the folder contains service.did.js, which causes index.js to be unable to find backend.did.js.

Why is this the case?
Do I have to rename the backend canister to service to avoid conflicts? That seems odd.

1 Like

can you provide a little bit more context?

  • dfx version
  • steps to reproduce (new project, … )

did you do any modifications or does this issue happen with the default project?

image
dfx version: 0.24.3
Projects created with dfx new still have this issue, all generated did files are named service.did, rather than the canister name.:
dfx.json:

{
  "canisters": {
    "dfx_test_backend": {
      "candid": "src/dfx_test_backend/dfx_test_backend.did",
      "package": "dfx_test_backend",
      "type": "rust"
    },
    "dfx_test_frontend": {
      "dependencies": [
        "dfx_test_backend"
      ],
      "source": [
        "src/dfx_test_frontend/dist"
      ],
      "type": "assets",
      "workspace": "dfx_test_frontend"
    }
  },
  "defaults": {
    "build": {
      "args": "",
      "packtool": ""
    }
  },
  "output_env_file": ".env",
  "version": 1
}

I do not know why those files are there. You shouldn’t really rely on files in .dfx. Ideally, you would use the files generated by dfx generate, which are by default in src/declarations.

dfx generate is the one that will generate the correct did file
But I would prefer to generate the correct did file in the .dfx folder during dfx deploy, instead of having to use dfx generate separately every time I update the code.

Our default templates run dfx generate as part of the prebuild step of npm run build. Then you don’t have to do anything manual. Is that not an option for you?

My actual use case is:

The front end imports the did.js file from the .dfx folder in the back end as a type restriction.

Originally, I expected to rely on the did.js file automatically generated by dfx deploy, but now the .dfx folder generated by dfx deploy does not match the project name set in dfx.json.

For example, the back end canister name is backend, but the generated file is called service.did.js.