mdgriffith / elm-codegen

https://package.elm-lang.org/packages/mdgriffith/elm-codegen/latest/
BSD 3-Clause "New" or "Revised" License
138 stars 16 forks source link

Install codegen/helpers/LocalFile.elm fails on run #41

Open absynce opened 2 years ago

absynce commented 2 years ago

Overview

Installing a local file inside of a directory fails when run is called.

This follows the pattern in the install help guide:

image

SSCCE project: https://github.com/absynce/elm-codegen-local-import-error.

Expected behavior

elm-codegen install codegen/helpers/LocalFile.elm creates helper. Then it runs with elm-codegen run.

Actual behavior

elm-codegen install codegen/helpers/LocalFile.elm creates helper. Then it errors with elm-codegen run.

To reproduce inside SSCCE project

elm-codegen run

Error

Error: ENOENT: no such file or directory, open 'generated/codegen/helpers/LocalFile.elm'

Full stacktrace

node:fs:585
  handleErrorFromBinding(ctx);
  ^

Error: ENOENT: no such file or directory, open 'generated/codegen/helpers/LocalFile.elm'
    at Object.openSync (node:fs:585:3)
    at Object.writeFileSync (node:fs:2155:35)
    at _loop_1 (/Users/absynce/.npm/_npx/be14e040090c2b5c/node_modules/elm-codegen/dist/run.js:521:16)
    at copyHelpers (/Users/absynce/.npm/_npx/be14e040090c2b5c/node_modules/elm-codegen/dist/run.js:540:9)
    at Command.<anonymous> (/Users/absynce/.npm/_npx/be14e040090c2b5c/node_modules/elm-codegen/dist/run.js:706:13)
    at step (/Users/absynce/.npm/_npx/be14e040090c2b5c/node_modules/elm-codegen/dist/run.js:71:23)
    at Object.next (/Users/absynce/.npm/_npx/be14e040090c2b5c/node_modules/elm-codegen/dist/run.js:52:53)
    at /Users/absynce/.npm/_npx/be14e040090c2b5c/node_modules/elm-codegen/dist/run.js:46:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/absynce/.npm/_npx/be14e040090c2b5c/node_modules/elm-codegen/dist/run.js:42:12) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: 'generated/codegen/helpers/LocalFile.elm'
}

System info:

node -v
v16.14.0

elm-codegen -V
0.1.0
absynce commented 2 years ago

Workaround

If you only want to install a few files in a folder, but not all, move those files to a new folder and install the folder.

elm-codegen install codegen/helpers-to-install/
mdgriffith commented 2 years ago

Mm, good catch!

I wonder if I should just remove the recommendation to install single files. Installing a whole folder seems to be more sustainable goin forward. Thoughts?

absynce commented 2 years ago

It would simplify things by using the built-in configuration Elm provides with the source-directories config. Especially since the installed file/folder is copied into generated.

My original intent was to install a module from src/. I realized there's a second problem with that. Since I have both src/ and generated/ in my source folders, it finds two (2) of the module with the same name. By using a separate folder, I can withhold it from the main application's source-directories config, knowing it will be copied into generated/ instead.

I don't know if it would be within scope, but it might be helpful if the CLI were to throw an error if someone tries to install a file or folder that comes from the main application's source-directories. Then it could suggest this alternative approach.

abradley2 commented 1 year ago

My solution has been to move the root files into the traditional src directory and to just copy-pasta things around after any elm-codegen install.

Moving things around to a folder to install as a whole helps but I still get a warning from either Elm or Elm Language Server that I have a nested source-directory setup

EDIT I think what I'm referring to is a slightly separate issue.