How to prevent `anchor build` from updating Cargo.lock automatically? - rust

I was following the tutorial with its repo. After running anchor build, I noticed a few files have been added/updated so I could not pull the repo anymore. I tried to revert any changes by deleting the target folder and removed the package.json, yarn.lock, and node_modules.
Then I found whenever I undo changes in Cargo.lock (the one at the repo root), it would add the changes back by itself as if it refuses to revert to the repo's commit version!
Why? How can I do this?

Related

git status is not showing my changed files

I have a Node.js project and have imported a few Node modules. The git_status command shows the files that I changed in the project. I have also changed some files under the node_modules directory but those are not shown. The node_modules directory is shown as untracked.
Untracked files:
(use "git add <file>..." to include in what will be committed)
node_modules/
src/js/main-release-paths.json
I have changed only one file in node_modules:
node_modules/#oracle/grunt-oraclejet/node_modules/#oracle/oraclejet-tooling/lib/serve/connect.js
How can I track this file?
You shouldn't add the node_modules folder to git then it's managed bei the npm command. So remove that folder and use the npm install command.
When you have added something in that folder it's shown as changed, if you had first added some files. So i hop that folder is in your .gitignore file and ignored. You should check that.
Assuming you have git repo, Please check the .gitignore files inside your project. Entries in .gitignore will be ignored by Git and will not show up when you do git add.
Adding the file to track
This is most likely not a suitable approach (read below). However , if you really want to do it:
In order to track a file nested under node_modules, you should add the directory. All of its content will be staged in git
git add node_modules
Recommended approach
You probably don't want to track the contents of node_modules directory, because:
running npm install will wipe your changes
hundreds or thousand files are likely to end up present in that directory
tracking package.json and package-lock.json in git is sufficient, then populate node_modules using npm install command.
You are not supposed to modify files located in node_modules directory directly. Instead, fork the module in question, modify it, and:
either publish your own version in npm
or reference it as a git repository inside package.json (how-to)
Then, also add node_modules directory to the .gitignore file.
If the whole directory is untracked, git status will show only the directory. Because it's untracked, there is no meaningful difference to git between changed and unchanged files in there.
Other answers have already addressed that tracking node_modules is nota good idea. Of you want to track your changes to modules, consider cloning the module repo and including it as a git submodule.
It should be inside .gitignore file. A .gitignore file should be committed into your repository, in order to share the ignore rules with any other users that clone the repository.

Why does "npm install" modify package-lock.json? Why commit it to git then?

When I run "npm install" in a project it often modifies package-lock.json, for example if I work on the same project from another computer (with different node or npm version).
But at the same time the documentation suggests that the file is supposed to be added to version control (git in my case):
https://docs.npmjs.com/files/package-lock.json
This file is intended to be committed into source repositories, and
serves various purposes: ...
So should I commit the changes made by npm back and forth when switching work machines or when somebody else does npm install? This would be a nightmare.
Currently I just discard any changes to package-lock.json made by npm, and it's been working fine. So I might as well add it to .gitignore...
Am I doing it wrong? Should I use npm ci instead? I wouldn't call my computer a "CI", it's just a development machine, why should I use it there?
Basically I have the same question as this gentleman:
https://github.com/npm/npm/issues/18103#issuecomment-370401935
(Sadly I can't add a comment on that issue or create a new issue at all, the npm repo has issues disabled)
Yes you want to commit your package-lock.json file to source control. The reasoning behind this is to ensure that all of the same versions of each package are downloaded and installed for each user that pulls down the code. There are some other reasons to include the file such as tracking changes to your package tree for auditing.

Create specific node_modules directories for different git branches

I've done few searches before posting but none of them helped me.
I hold a git repository for my personal website, and I have a small problem when checking out one of its branches. There are 3 branches for different tools or frameworks : the former master branch, which acts as the current version of my website, & the Elm and Vue ones, that I'm working on at the same time. Each of these branches contains a package.jsons file and so a node_modules directory. My problem is : after checking out a branch, I have to 'yarn install' my packages to run the dev mode or build the app (with webpack), because of all the conflicted packages hosted in the same directory on different versions I guess.
It's a bit tiring to reinstall the required packages each time I checkout a branch. Moreover, I have then to commit the updated yarn.lock file.
So I am wondering if there is a way to create a specific node_modules folder or sub-folder, one for each branch, in order to have "scoped" directories. Or should I better separate this repo in 3 repos ?
I hope I am clear about this problem.
Thanks for reading !
You can commit node_modules so git will switch them on git checkout.
Or you can create separate worktrees. Checkout different branches in different worktrees and don't switch branches afterwards.

How to commit NPM module Names to git without the actual files so that they can get rebuilt at the other end?

I'm developing a node project which depends on several npm modules. What I'm currently doing is committing all those modules to my git repository, pulling on my server, and then rebuilding the modules on the other end because the system architectures are different. What I would like to do instead, is just commit enough stuff so that npm knows what it needs to rebuild, and nothing more.
I would hoping I could do this without actually committing all the module files. I know little about npm's internals, but see that every module has a package.json file, is this all that npm will need? And if that's the case, how would I go about ignoring all files in my node_modules folder except the package.json files?
Thanks.
Why can't you just have a package.json file in your main application root listing all your dependencies? This file should then be the only thing checked in to source control.
At build time or when other developers pull the code, npm install should be run from the same directory that has your package.json file. It will then pull down all your dependencies locally.

Heroku does not update node.js > package.json Github tarball dependecies

I am managing a dependency at Github which I use as a dependency in my project. I placed the dependecy as a tarball link (viz. https://github.com/username/dependecy/tarball/master) into the package.json and it is working fine as expected locally. When I update the package at Github I can run a npm install and all dependencies including the Github tarballs get updated. However it is not the case at Heroku. tarball-linked dependencies does not get updated. Any ideas?
I had the similar problem. My App had the dependency with caret versioning like this:
"dependency": "^0.6",
So every time the dependency got its patch version updated, I wanted Heroku to have the updated dependency without any commits/pushes to my App. For that I set just in case
heroku config:set NODE_MODULES_CACHE=false
And when the new patch version became available I did the manual redeploy of the same App that was already deployed from Heroku Dashboard.
Can you try to do the same for your case? Possibly this will help you.
Since no one has answered this yet I will share what I have learned. The trick is getting heroku to think the tarball is different or new so that it downloads it again. As #celalo suggested you can remove it or change the path, commit, push, change it back, commit and push. This is messy but it works.
What I ended up doing was making a master1 branch. I keep the branch in sync with master and then alternate the tarball url between master and master1 when I need it to update.

Resources