Plugin stopped writing file

Hello,

this is my first post in the forum. I have a very simple plugin that writes a file with production configuration (from env variables) to the final directory (angular app).

This is the plugin

const fs = require('fs').promises;
module.exports = {
  onSuccess: async ({ utils, constants }) => {
    const filePath = `${constants.PUBLISH_DIR}/assets/config.json`;
    const environment = {
      apiUrl: process.env.MY_API_URL,
      keycloakConfig: {
        url: process.env.AUTH_URL,
        realm: process.env.REALM,
        clientId: process.env.CLIENT_ID
      }
    };
    console.log('Vars to write', JSON.stringify(environment));
    console.log(`Creating file ${filePath}`);
    try {
      await fs.writeFile(filePath, JSON.stringify(environment));
      console.log(`File ${filePath} created correctly`);
    } catch (error) {
      return utils.build.failBuild(`Error when creating the file ${filePath}`, {
        err
      });
    }
  }
};

This has been working since forever, but a couple of days ago, I did a change to my app, I triggered a deploy and the file is not being created anymore, and the log shows that it was succesfully created with the correct variables printed in the log.

I tested by uploading the default config.json file which points to localhost and I get deployed the localhost version, I also tried deleting the file from the repo to see if it was being overwritten and nothing, the file is simply not being created. I haven’t changed anything in a long time so probably something changed in netlify that I’m not aware of.

I checked a couple of changelogs but I can’t find anything specific to my case and I’m not even sure if that is the case.

I apologize in advance if this is something well known by everyone.

Thanks in advance.

Hey @ericmartinezr,
First of all, welcome to the forum! Sounds like a great Build Plugin and sorry to hear that it stopped working.

To make sure I understand, you say:

the file is not being created anymore, and the log shows that it was succesfully created with the correct variables printed in the log.

So the plugin seems to be logging the right vars (console.log('Vars to write', JSON.stringify(environment));) but then does not write the file and does not throw any errors? Does this result in your build failing, or how do you know the file wasn’t actually written?

Last request: could you please share your Netlify URL so we can take a look?

1 Like

Hey Jen, thanks for answering.

You’re right, it logs the values but it doesn’t write the file at all and I know because the default one, the one I use for development is being deployed (pointing to localhost). And no, it doesn’t fail at all nor threw warnings/errors.

I tried locally with Netlify CLI and I did get a few errors there that I thought at first would be the cause but they weren’t. The CLI threw these:

  1. As you can see in the code I posted initally, the catch block has error in the parenthesis and err in the block inside catch. It reached the catch block because the paths aren’t the same locally, so it wasn’t able to write the file. This wasn’t the cause.
  2. Then it suggested the following
Plugin error: since "onSuccess" happens after deploy, the build has already succeeded and cannot fail anymore. This plugin should either:
- use utils.build.failPlugin() instead of utils.build.failBuild() to clarify that the plugin failed, but not the build.
- use "onPostBuild" instead of "onSuccess" if the plugin failure should make the build fail too. Please note that "onPostBuild" (unlike "onSuccess") happens before deploy.

In this case I changed to failPlugin.

I fixed these locally and it worked (I had to fix some path issues locally but had to undo them when I uploaded the plugin fixed), I saw that the config file was written.

That’s all I can say about this. Debugging it it’s a little bit complicated and I have no more info to share (I think).

Thanks again for answering. If you need more info, let me know.

Hey there,
Ah, right, I think this is related to the recent change in when onSucess and onEnd build events happen. Here’s a post about the change, which shipped about 2 weeks ago:

Can you try changing onSucess here: onSuccess: async ({ utils, constants }) => { to onPostBuild? Seems like that should do what onSuccess used to do:

Let us know how it goes!

1 Like

@jen thank you so much for your help. That was indeed the issue. Changing from onSuccess to onPostBuild solved the problem.

Thanks again for your help!!

1 Like

Awesome, glad to hear all is resolved!