Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using functions of an included file makes coldfusion forget imports. Is this normal?

I noticed that when I call a function of a previously included (cfinclude) .cfm file, all the coldfusion imports (cfimport) that have happened at this point are forgotten. It's like you haven't imported anything. I found this very strange behavior so I've isolated it, but the results stayed the same, even in coldfusion 10.

My setup:

/example

  • functions.cfm
  • index.cfm
  • /components
    • MyCFC.cfc

functions.cfm:

<cfscript>
  function test(){
    return "test";
  }
</cfscript>

components/MyCFC.cfc:

component  output="false"{}

index.cfm:

<cfscript>
  include "functions.cfm";
  import components.MyCFC;
foo = test(); bar = new MyCFC(); </cfscript>

This code will throw a coldfusion error : "Could not find the ColdFusion component or interface MyCFC". when foo = test(); is removed or placed after bar = new MyCFC();, the code runs just fine.

It doesn't matter if the import is placed before or after the include. Whenever an included function is called, the imports are forgotten.

Is this this a bug or is it supposed to behave this way?

Tested in coldfusion 9,0,0,251028 and coldfusion 10,282462

like image 454
jan Avatar asked Oct 06 '22 13:10

jan


1 Answers

Since I do a lot of imports, I recently met some odd behavior and reported it do the CF9-bugbase: https://bugbase.adobe.com/index.cfm?event=bug&id=3288035

ColdFusion resolves the imports only for the current file and whenever you call a different file, it's like the "execution context" switches to that files with it's imports. So in your case when you execute new ColdFusion looks in functions.cfm for the imports. When you then call a method of the current file, it switches back and finds the import.

If I am right, your code should work, if you execute bar = new MyCFC(); directly after the import. Or you could define another method in the index.cfm and call it, before you create the class.

To work around that bug you need to make sure, that the import is resolved (on first use in the objects lifecycle) before that "context switching" takes place. So call new MyCFC(); before the external method.

@Adobe: Would be nice to fix this;)

like image 166
Walter Seethaler Avatar answered Oct 10 '22 01:10

Walter Seethaler