jhipster / generator-jhipster

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.
https://www.jhipster.tech
Apache License 2.0
21.54k stars 4.02k forks source link

JHipster generating all files again while switching OS #17115

Closed sanjeevsree closed 2 years ago

sanjeevsree commented 2 years ago

All the entities are re-generated again when I run my code after switching my OS from mac to windows.

I tried to update a single entity using jdl. It works fine in mac whereas when I try the same in windows environment all the entities are getting regenerated again.

I am getting that the entities are identical in my mac system but it is asking to be overwritten in windows.

Tried running inside an Ubuntu container but the result is the same. This causes a lot of change files in git and makes the development slow

jhipster version: 7.3.1

gmarziou commented 2 years ago

What are the differences? Have you run git diff to check? Could it be a misconfiguration of git for handling end of lines?

sanjeevsree commented 2 years ago

The git diff shows no change but it is time-consuming effort to provide yes or no input for all the files which it asks for overwriting

vishal423 commented 2 years ago

Can you provide more details on conflicting file types? Have you modified generated .gitattributes?

sanjeevsree commented 2 years ago

When i try modifying an entity in windows it asks for overwriting all files for all entities

pip25 commented 2 years ago

I also encountered the problem during entity generation, the cause seems to be that line ending differences (LF vs. CRLF) are treated by JHipster as conflicts while checking the existing files.

pascalgrimaud commented 2 years ago

So I think it is related to git configuration

pip25 commented 2 years ago

Not necessarily. Forcing LF line endings in Git does make the problem go away, but that is only a workaround in my opinion. The real problem here is that the JHipster generator wants to create code with LF line endings regardless of the OS it runs on.

mshima commented 2 years ago

The fix would be something like this:

    const convertToCRLF = () =>
      patternSpy(file => {
        file.contents = file.contents.replace(/!\r\n/g, '\r\n\');
      }, '**/*.java').name('jhipster:crlf');

And inject

...(isWindowsOs ? [convertToCRLF()] : []),

below here: https://github.com/jhipster/generator-jhipster/blob/78f3624e66d1a12c0a45715e557bb2d26e8b59e5/generators/bootstrap/index.js#L197

pascalgrimaud commented 2 years ago

The generator exists since more than 5 years, and as far as I remember, we don't have this kind of problem.

Adding this work around can change all projects of our Windows users.

Adding a new question / flag would be over killed.

pip25 commented 2 years ago

@pascalgrimaud, JHipster actually had similar issues already (see https://github.com/jhipster/generator-jhipster/issues/11231 and https://github.com/jhipster/generator-jhipster/pull/11205). Maybe the previous fix broke somehow?

mshima commented 2 years ago

Updated fix:

    const convertToCRLF = git =>
      patternSpy(async file => {
        const result = await git.raw('check-attr', 'eol', '--', file.name);
        if (result.includes('crlf') {
          file.contents = file.contents.replace(/!\r\n/g, '\r\n\');
        }
      }, '*').name('jhipster:crlf');

And inject

...(isWindowsOs ? [convertToCRLF(this.createGit())] : []),

below here: https://github.com/jhipster/generator-jhipster/blob/78f3624e66d1a12c0a45715e557bb2d26e8b59e5/generators/bootstrap/index.js#L197

And change https://github.com/jhipster/generator-jhipster/blob/78f3624e66d1a12c0a45715e557bb2d26e8b59e5/generators/bootstrap/index.js#L104 to include .gitattributes so it will be committed to disk before using it.

pblanchardie commented 2 years ago

@mshima being on Windows does not mean that we want files to be generated with CRLF. When git core.autocrlf is false, LF files are not automatically transformed to CRLF and I think it's better when everyone use LF both remotely and locally to avoid that kind of issue.

I did not check what have been done in the past to handle that in generator-jhipster, my 2 cents.

The specific problem mentioned by @sanjeevsree seems to be caused by either having remote CRLF files (which is not advisable) or local CRLF files (which is not compatible with JHipster generated files, apparently).

IMHO the best workaround would be to fix her/his own local configuration and/or remote files, instead of making JHipster force CRLF for all Windows users, ignoring their git configuration, which is an opinionated "breaking" change.

In JHipster Lite, we now detect line endings used in the project to see if it needs CRLF, and it defaults to LF.

The simplest thing would be to recommend using LF in the README :)

Zacorich commented 2 years ago

Both git and jhipster do one similar task and this is creating brand new files on target OS. Git does the job well so why jhipster shouldn't? I think solution by https://github.com/jhipster/generator-jhipster/issues/17115#issuecomment-986118493 is a nice compromise.

Git persists files with LF. JHipster generates files with LF. Most IDEs can edit files with LF on Windows, but that must be configured.

Changing JHipster generator to be aware of the OS and generate CRLF for Windows and LF for everything else would actually solve a problem of extra configuration needed on Windows to use LF for all files that JHipster generates. But on the other hand the only place you might need CRLF on windows are BAT files or other scripts that are required to have CRLF by Windows. Also most application apart from your heavyweight smart IDE will try to render CRLF on Windows and when just LF is found it might introduce a problem. That's why git implemented its detection algorithm to checkout files correctly on Windows that can be also fine tuned in .gitignore file.

mbrandl87 commented 1 year ago

Hi,

I know this topic is quite old but I'm having said problem right now.

As JHipster thinks it has to regenerate everything even I just add a field to one entity, probably because of git wrote the files with CRLF on checkout, I tried to stick with LF.

However Eclipse changes half of the files back to CRLF when the Maven Builder is triggered despite I configured the workspace to use LF.

So that ofcourse leads again to unstaged non-changes, JH regenerating the not really changed files again etc.

Can anyone give me a hint how to cope with that? :D Do I really have to apply dos2unix to all changed files before firing up the JHipster generator again? What happened to @mshima's PR, using JH 8 beta 3 it doesn't seem to be there at least.

Best Martin

mshima commented 1 year ago

@mbrandl87 the transform is there: https://github.com/jhipster/generator-jhipster/blob/f8a926b6cb854679278026a784db57edd28670a8/generators/bootstrap/generator.mts#L191

You should open a new issue if it’s not working.