question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Addon is unable to modify app files using preprocessTree

See original GitHub issue

I use preprocessTree to manipulate app files like this:

const stew = require('broccoli-stew');
const themeName = process.env.EMBER_THEME || 'default';

module.exports = {
  name: require('./package').name,

  preprocessTree(type, tree) {
    if (type === 'css' && themeName !== 'default') {
      tree = stew.rm(tree, `app/styles/config/themes/_current-theme.scss`);

      tree = stew.mv(
        tree,
        `app/styles/config/themes/_${themeName}.scss`,
        `app/styles/config/themes/_current-theme.scss`
      );

      tree = stew.rm(
        tree,
        `${this.app.name}/styles/config/themes/_current-theme.scss`
      );

      tree = stew.mv(
        tree,
        `${this.app.name}/styles/config/themes/_${themeName}.scss`,
        `${this.app.name}/styles/config/themes/_current-theme.scss`
      );

      return stew.debug(tree);
    }

    return tree;
  },
};

This is super straightforward: I remove _current-theme.scss and replace it with another Sass file.

From the debug tree output I can see that the substitution happens successfully.

Yet, the resulting CSS stylesheet does not reflect the substitution.

@simonihmig has figured out that this may be because the addon’s tree gets overwritten with the app tree, so all changes that the addon makes are discarded.

The only exception are new files which don’t exist in the app tree. Those don’t get overwritten. But we can’t rely on a file that does not exist in the app codebase because the IDE is unable to resolve references:

image

I don’t expect the core team to recognize this as a bug, but from an addon author’s perspective it surely feels as a bug!

How do I make my addon manipulate app files in the build?

PS Why are there two copies of styles in a tree: app/styles/ and my-app-name/styles/?! Which one is the right one?


Output from ember version --verbose && npm --version && yarn --version:

ember-cli: 3.28.0
node: 12.14.1
v8: 7.7.299.13-node.16
uv: 1.33.1
zlib: 1.2.11
brotli: 1.0.7
ares: 1.15.0
modules: 72
nghttp2: 1.40.0
napi: 5
llhttp: 2.0.1
http_parser: 2.8.0
openssl: 1.1.1d
cldr: 35.1
icu: 64.2
tz: 2019c
unicode: 12.1
os: linux x64
npm: 7.11.2
yarn: 1.22.4

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
simonihmigcommented, Sep 29, 2021

Impressive investigation, thanks @lifeart!

1reaction
lolmauscommented, Sep 29, 2021

@lifeart Thank you for your research and proposed fixes! 🙏

So @lifeart discovered that the root cause of the issue is this line in Sass which makes cwd (current working directory) to always be the first include path. This behaviour is comment-documented here.

Unfortunately, changing that would be severely breaking. Sass relies on that behaviour similar to how Ember CLI relies on that app files always have priority over addons’ app/ files. For this reason, we did not attempt creating a PR or even an issue in Sass.

But this cwd behaviour makes no sense in ember-cli-sass. In ember-cli-sass, cwd should be pointing at the Broccoli tree, not the project codebase. @lifeart has created an upstream issue: https://github.com/simonexmachina/broccoli-sass-source-maps/issues/30

For this reason, the easiest solution could be changing cwd in broccoli-sass-source-maps, rather than creating a custom importer (which must support importing from node_modules/ in addition to the Broccoli tree, which requires extra effort to reimplement).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to write files into the app tree in ember-cli ...
The next phase is integrating it into an ember-addon, and I created one with a preprocessTree hook. module.exports = { name: 'route-template- ...
Read more >
Allow or restrict add-ons in Docs editors - Google Support
Drive and Docs.​​ Click Features and Applications. Click Add-Ons. Check the Allow users to install Google Docs add-ons from add-ons store box. Click...
Read more >
Unable to View or Edit Office files by using Office Online or ...
Cause. This issue occurs because the operation that you requested in Office for the web or Office Web Apps does not support the...
Read more >
Can't edit files in Confluence Server using Atlassian ...
Problem. Confluence Server and Data Center users are unable to edit attached files using the Atlassian Companion app. When they click Edit ......
Read more >
cordova-plugin-file
Your app should remove files from this directory when it is applicable, ... Note: If external storage can't be mounted, the cordova.file.external* ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found