Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change in .clasp.json location & behavior with --rootDir #869

Open
therealchjones opened this issue Aug 16, 2021 · 7 comments
Open

Change in .clasp.json location & behavior with --rootDir #869

therealchjones opened this issue Aug 16, 2021 · 7 comments

Comments

@therealchjones
Copy link

Steps to reproduce the problem

  1. Install clasp locally
  2. Create a project using --rootDir
  3. Attempt to push files

Expected Behavior

(as in 2.4.0:)
4. Success

% mkdir -p project-2.4.0/{src,tools}
% cd project-2.4.0/tools
% npm install @google/clasp@2.4.0
...
% ./node_modules/.bin/clasp create --type webapp --title test-2.4.0 --rootDir ../src
Created new webapp script: https://script.google.com/d/.../edit
Warning: files in subfolder are not accounted for unless you set a '.claspignore' file.
Cloned 1 file.
└─ ../src/appsscript.json
% ./node_modules/.bin/clasp push
└─ ../src/appsscript.json
Pushed 1 file.

Actual Behavior

(in 2.4.1:)
4. .clasp.json file (now in rootDir rather than same directory) not found; must run with clasp -P rootdir

% mkdir -p project-2.4.1/{tools,src}
% cd project-2.4.1/tools 
% npm install @google/clasp@2.4.1
...
% ./node_modules/.bin/clasp create --type webapp --title test-2.4.1 --rootDir ../src   
Created new webapp script: https://script.google.com/d/.../edit
Warning: files in subfolder are not accounted for unless you set a '../src/.claspignore' file.
Cloned 1 file.
└─ ../src/appsscript.json
% ./node_modules/.bin/clasp push   

No valid /Users/chjones/Downloads/project-2.4.1/tools/.clasp.json project file. You may need to `create` or `clone` a project first.
% ./node_modules/.bin/clasp -P ../src push
└─ ../src/appsscript.json
Pushed 1 file.

Specifications

  • Node version (node -v): 16.5.0
  • Version (clasp -v): 2.4.1
  • OS (Mac/Linux/Windows):Mac

Presumably this is associated with #865 ; the new behavior may be the "correct" one, but failure to create .clasp.json in the working directory is a breaking change when that is expected (at least, for some of my scripts. :-) ) Is this worth specifying in documentation or release notes?

Thanks as always,
CDJ

@therealchjones
Copy link
Author

Additional issue: as the relative directory rootDir is also written to .clasp.json as given to the --rootDir option., clasp -P must also be called from a directory at a certain hierarchy.

From the above, if we are in the tools directory and use clasp create ... --rootDir ../src to place the project in the given src directory, ../src is written to .clasp.json, hence moving up a directory level and calling clasp -P src/ ... results in:

% clasp -P src/ push
ENOENT: no such file or directory, open '../src/appsscript.json'

If we can agree on desired behavior for --rootDir and -P I'm happy to work on a PR. Thanks again!

@caxon
Copy link

caxon commented Sep 23, 2021

also noticed this behavior breaking my app (built on https://github.com/enuchi/React-Google-Apps-Script) when npm decided to bump from clasp version 2.4.0 to 2.4.1! Might confuse others, especially if the breaking change comes from a minor version bump!

@tresorama
Copy link

I figured out that:

  1. clasp push need a .clasp.json to know what to do.
  2. clasp create ... --rootDir ./dist create .clasp.json automatically and store it at ./dist/.clasp.json
    • BUT IF ./dist DIRECTORY DOES NOT EXIST CLASP WON'T CREATE THE FILE.

IF YOU ARE CREATING A NEW PROJECT

( Google Sheets as Container in this example )

It's better you initialize the project with :

cd ~/Desktop
mkdir my-gas-project
cd my-gas-project
clasp create --type sheets --title "MySheetsContainer" --rootDir .

This let clasp create the .clasp.json in the current working directory.

Then, assumed your code bundler (ie. webpack ) put your code inside ./dist, you need to manually update the .clasp.json :

{
  "scriptId": "1AFqeTOrlAA7TjcbAuW9Pqt_cxPVliUfeagV1YDS9WNHJK62RD8G2Lwt0",
  "rootDir": "./dist",  // <- HERE PUT YOUR BUNDLE DIRECTORY
  "parentId": ["1lViSwkMtrnJcYSvi9NcvmO2OamJzgXz_dUoXNn0SxVQ"]
}

IF YOU'VE ALREADY CREATED A PROJECT

In this case you should not have the .clasp.json file.

So create .clasp.json and put in project root directory , or the directory from where you call clasp pull | push:

{
  "scriptId":"1AFqeTOrlAB7TrcbAuW9Pqt_cxPVliUfeagV1YDS9WNHJK62RD8G2Lwt0",
  "rootDir":"./dist", // <- HERE PUT YOUR BUNDLE DIRECTORY
  "parentId":["1lViSwuMtrnJcYHvi9NcvmO2OamJzgXz_dUoXNn0SxVQ"]
}

@ordinaryandrasonable
Copy link

I did the following:

  1. Install clasp locally
  2. Clone a project from /myproject using --rootDir src
  3. Attempt to push files

Actual Result:
"No valid [full path] /myproject/clasp.json project file. You may need to create or clone a project first."

The .clasp.json project file was created in the /myproject/src.

Move the .clasp.json file
I moved the .clasp.json file from /myproject/src to /myproject.

Running clasp push from /myproject now works as expected.

@khudoyorrahmatov
Copy link

I did the following:

  1. Install clasp locally
  2. Clone a project from /myproject using --rootDir src
  3. Attempt to push files

Actual Result: "No valid [full path] /myproject/clasp.json project file. You may need to create or clone a project first."

The .clasp.json project file was created in the /myproject/src.

Move the .clasp.json file I moved the .clasp.json file from /myproject/src to /myproject.

Running clasp push from /myproject now works as expected.

That worked for me too. Thanks a lot!

@HoldYourWaffle
Copy link
Contributor

I doubt this is the correct behavior, at least it's very counter-intuitive (#923).

I think "specified directory" in #832 was misunderstood to mean "specified by --rootDir", but rootDir is documented as:

Local directory in which clasp will store your project files.

I'd interpret "project files" as "pushed files", as in telling clasp which subdirectory should be pushed, analogous to TypeScript's include.
As far as I can tell this is what it was intended for (#22 / #25), how it's commonly used and how it functions. The example in the README seems to support this, listing a build folder for rootDir. The README also says:

When running clone or create, a file named .clasp.json is created in the current directory to describe clasp's configuration for the current project.

This is no longer true, .clasp.json is now created in the provided rootDir.

The suggested workaround with -P seems unnecessarily tedious and undermining the purpose of rootDir, not to mention hazardous with shenanigans around relative paths as mentioned by @therealchjones.

Proposal

I propose the following changes to create, clone and rootDir:

  1. A relative rootDir in .clasp.json is always resolved against the location of .clasp.json to avoid shenanigans.
    A relative --rootDir passed to create and clone should still be resolved against the current working directory.
  2. The create and clone commands always put the new .clasp.json in the current working directory, containing a relative path to the passed rootDir if applicable. The created/cloned files are put inside --rootDir.
  3. Rename rootDir to something like sourceDir, buildDir, distDir, synchronizedDir or gapDir to clarify its function and make its purpose more explicit. rootDir could stay as an alias with a deprecation warning for now to avoid a breaking change.

I'd love to hear your thoughts on this! I'll look into creating a draft PR to see how this plays out.

@sekoyo
Copy link

sekoyo commented Jul 16, 2024

If I specify a rootDir it pushes the files but doesn't resolve them to be at the base, uploading them in a dist folder.

└─ dist/appsscript.json
└─ dist/dialog.68d5e797.js
└─ dist/dialog.html
└─ dist/main.js
└─ dist/sidebar.15d4dce8.js
└─ dist/sidebar.html
Pushed 6 files.

Hence it's useless as the files don't appear in App Script.

In the end I copied .clasp.json into dist/ during build and ran it from there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants